【发布时间】:2015-10-14 03:34:25
【问题描述】:
我正在尝试通过 Ajax 调用从 django 模板调用我的视图。
我希望表单对象响应视图,以便我可以通过 jquery 在 div 元素中呈现此表单。
有可能吗?怎么样?
这是我尝试过的:
home.html
function get_edit_form(button, id)
{
$.ajax({
url: "/manage/licenses/{{mls_signup_code}}/{{agent_id}}/" + id + "/",
type: "get",
data: {id: id},
success: function(response) {
console.log(response);
$("#formdiv").html({{ response.as_p }});
}
})
}
Views.py
elif request.method == "GET":
owned_license_id = request.GET.get('id', '')
form = OwnedLicenseForm(owned_license_id)
return form
【问题讨论】:
-
你试过了吗?
-
@Gocht:请检查,编辑
-
我猜这会引发一个序列化错误,你应该返回一个 json。为什么需要将表单作为对象传递?
-
不,它没有,它什么都不做,只是在控制台对象中显示此错误没有属性'status_code,我明白为什么。我尝试使用 return HttpResponse(form) ,但我没有看到任何表单标签,它只呈现表单值
-
你需要HttpResponse,See
标签: python django django-templates