【发布时间】:2019-03-10 15:20:46
【问题描述】:
我遇到了在 Django 中使用 Ajax 将主键值传递给动态 url 的问题。感谢您的帮助...
下面是点击按钮功能的JS代码:
$(function () {
$(".js-create-book").click(function () {
$.ajax({
url: '/supplier/**<int:pk>**/new/',
type: 'get',
dataType: 'json',
beforeSend: function () {
$("#modal-book").modal("show");
},
success: function (data) {
$("#modal-book .modal-content").html(data.html_form);
}
});
});
});
views.py 得到的函数 new_stok() 如下:
def new_stok(request, pk):
supplier = get_object_or_404(Supplier, pk=pk)
form = NewStokForm()
context = {'pk': supplier.pk, 'form': form}
html_form = render_to_string('includes/partial_stok_create.html',
context,
request=request,
)
return JsonResponse({'html_form': html_form})
【问题讨论】:
-
id 是从哪里来的?