jsで取得した結果をhtmlに渡したいです。
<body> <form method="post" action=""> <select id="mainDD" data-placeholder="Choose" class="chzn-select" style="width:600px;"> {% for i in json_data.items.values %} <option value="{{forloop.counter}}">{{ i }}</option> {% endfor %} </select> {% for key, values in preprocessed %} <select name="type" id=type{{forloop.counter}}> {% for counter, value in values %} <option value="{{forloop.counter}}">{{ value }}</option> {% endfor %} </select> {% endfor %} </form> <script type="text/javascript"> $(document).ready(function () { $('#mainDD').on('change', function() { var thisType = "type" + $(this).val(); for(i=1; i<6; i++) { var thisId = "type" + i; if(thisType !== thisId) { $("#"+thisId).hide(); } else { $("#"+thisId).show(); } } }).trigger('change'); }); </script> <form id="postform" action="http://localhost:8000/app/test_view" method="POST"> {% csrf_token %} <input type="submit" value="SEND"> </form> <script type="text/javascript"> $('[name=type]').change(function() { var array1 = []; var array2 =[]; $('[name=main] option:selected').each(function() { array1 = $(this).text(); console.log(array1); }); $('[name=type] option:selected').each(function() { array2 = $(this).text(); console.log(array2); }); }); </script> </body>
とindex.htmlに書きました。
このarray1とarray2で取得した内容をtest.htmlに渡してpタグ内で表示させたいです。
test.htmlには
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>選択結果</title> </head> <body> <p>{{ array1 }}</p> <p>{{ array2 }}</p> </body> </html>
と書きました。
views.pyには
from collections import OrderedDict from django.shortcuts import render import json from django.http import JsonResponse def index(request): with open('./data/company_demand.json', 'r') as f: json_data = json.loads(f.read(), object_pairs_hook=OrderedDict) preprocessed = [] counter = 0 for key in ["type1", "type2", "type3", "type4","type5"]: values = [(i + counter, value) for i, value in enumerate(json_data[key].values())] preprocessed.append((key, values)) counter = len(json_data[key]) return render(request, 'index.html', {'json_data': json_data,'preprocessed': preprocessed}) def test_view(request): return render(request, 'test.html')
と書きました。この時、test_viewメソッドにどのようにarray1とarray2で取得した内容を渡せば良いでしょうか?
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2017/09/28 03:09
2017/09/28 04:58
退会済みユーザー
2017/09/28 05:05
退会済みユーザー
2017/09/28 05:05
2017/09/28 05:11
退会済みユーザー
2017/09/28 05:17
2017/09/28 05:24