0
DjangoテンプレートでJQuery Datatableをレンダリングしようとして問題が発生しましたが、何も返されません。DjangoでJQuery Datatableをレンダリングすることができません
"tabden"ビューにあるすべてのコードを "ordenar"ビューの "else"ステートメントに置くと、JSONオブジェクトが完全に返されます。
後者の問題は、テンプレートに2つの異なるDatatablesがあり、2つのデータを区別するためにPOSTリクエストをどのように作成するかわかりません。だから私はDatatables要求を別々のビューに入れることに決めました。
views.py
def ordenar(request):
if request.method == 'POST':
if 'var1' and 'var2' and 'var3' in request.POST:
#doing something already
return HttpResponse("SUCCESS")
else:
return HttpResponse("NO SUCCESS")
return render(request, 'mypage/index.html', {'some':context})
def tabla(request):
query = MyModel.objects.all()
json = serializers.serialize('json', query, use_natural_foreign_keys=True)
return HttpResponse(json, content_type='application/json')
ジャバスクリプトテンプレート:
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.16/css/dataTables.bootstrap.min.css">
<script src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.10.16/js/dataTables.bootstrap.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#mytable").DataTable({
"ajax": {
"url": "{% url 'tabla' %}",
"type": "POST",
"dataSrc": ""
},
"columns": [
{ "data": "pk" },
{ "data": "fields.product" },
{ "data": "fields.weight" }
]
});
});
</script>
テンプレートHTML:URLで同じを指す複数のURL:
<div class="col-lg-6">
<table class="table table-strip" id="mytable">
<caption><center>My Caption</center></caption>
<thead>
<tr>
<th>Item</th>
<th>Product</th>
<th>Weight</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
はい、削除できます。 –