In Django, how do I return a form back to a Javascript function? -
in template, have javascript send call django method. in method return blank form template. here's javascript:
require(["dojo/request/xhr", "dojo/domready!"], function(xhr, ready){ var url = window.location.pathname + "dev/" + report_id + "/" + report_url + "/"; xhr(url, { method: "get" }).then( function(response){ var json_response = json.parse(response); //do stuff } ); });
here's what's happening in django view:
def my_view(request): if request.method == "get": form = myform() data = json.dumps({ 'form': form, }) return httpresponse(data, mimetype="application/json") else: #do other stuff
here's error i'm getting right now:
raise typeerror(repr(o) + " not json serializable") typeerror: <development.dev_forms.myform object @ 0x7f3a8dd07e50> not json serializable
so seems can't serialize django form. so, i'd return fields of form template somehow. other ways can this? there way html of fields? build dictionary myself. other suggestions? thanks!
you first need render form , pass ajax call:
ctx = {'form': myform()} data = { "form": render_to_string("app/form_template.html", ctx, context_instance=requestcontext(request)) } return httpresponse(json.dumps(data), mimetype="application/json")
then in form using json_response.form
in success
function of ajax call.
Comments
Post a Comment