![](/img/trans.png)
[英]How to display data from ajax response ( json type ) in Django template?
[英]How to display Ajax response data in Popup?
我有一些response
數據,但我想在彈出窗口中顯示該數據,請告訴我如何在彈出窗口中顯示 Ajax 數據。
這是我的views.py
文件...
def myview(request):
datas=TestForm.objects.all
template_name='test.html'
context={'datas':datas}
return render(request, template_name, context)
def myview(request, id):
display=TestForm.objects.get(pk=id)
template_name='test.html'
context={'display':display}
return render(request, template_name, context)
這是我的html
文件...
{% for a in datas %}
<a href="javascript:void()" class="btn btn-primary" onclick"exampleModal({{a.id)}})" data-url="{% url 'myap:list_single' a.id %}">
{{a.product_id}}
</button>
{% endfor %}
這是我的popup
代碼...我想在其中顯示 AJAX 數據...
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-
labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<tr>
<td>{{datas.name}}</td>
<td>{{datas.price}}</td>
<td>{{datas.category}}</td>
</tr>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
這是我的AJAX
代碼...
function exampleModal(id){
$.ajax({
url: $(this).attr("data-url")
type: 'get',
dataType: "HTML"
success: function(res) {
$('.exampleModal').html(res);
$("#exampleModal").modal("show");
}
});
}
function exampleModal(id){
$.ajax({
url: $(this).attr("data-url")
type: 'get',
dataType: "HTML"
success: function(res) {
$('#exampleModal .modal-body').html(res); //here is where the response is added to the body element of the modal
$("#exampleModal").modal("show");
}
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.