[Answer]-How to render django form into html (not in template)

1👍

All the template does when presented with any object is call str() (or unicode()) on it. Internally, the methods that implement those on the form class simply delegate to the as_table() method. So you can just call that directly:

return JsonResponse({'form': form.as_table()})

Note, though, that you might want to add other HTML, in which case a better approach might be to simply render a short template snippet, including the form, to a string – using render_to_string – and send that in the Json response.

0👍

You’ve gotta unload the form since it’s an object

if not form.is_valid():
    errors = form.errors  # You'll get a dict of errors
    return JsonResponse(data=errors)

From here use javascript to create new HTML.

Leave a comment