python - How to return django form object in an AJAX request from django template? -
i trying call view django template via ajax call.
i want form object in response view such can render form via jquery in div element.
is possible ? how?
this tried:
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
i see trying do, cannot render html form way:
$("#formdiv").html({{ response.as_p }});
i think confusing server side rendering (django templates) client side rendering. server side rendering happens when server processing request, cannot render objects produced javascript running in browser.
because response
javascript object, obtained jquery sending ajax request url. @ time, page has been rendered django's template engine, , sent browser. there no way django template aware of response
.
i understand want use as_p() method render form, can this:
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); // response form in html format $("#formdiv").html(response); } }) } # views.py elif request.method == "get": owned_license_id = request.get.get('id', '') form = ownedlicenseform(owned_license_id) return httpresponse(form.as_p()) # return html str
Comments
Post a Comment