Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

617
Views
Send Django Form as JSON via AJAX

I am working on project in Django 1.8.8 and I need to convert a django form into a JSON format so I can send it to the browser via an AJAX call.

I found this package ( https://github.com/WiserTogether/django-remote-forms ) which is no more available on Pypi. Beside it's 2 years old since the last commit.

Would you please give me some advice on what to do or which package to use ?

Thank you in advance for your help.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

The two main things you could do are:

  • Render the form to an HTML string, and send that.

  • Create a JSON object, from which the HTML can be constructed.

Here's an example of how you could turn a form object into json:

import json
def form_to_json(form):
    result = {}
    for name, field in form.fields.iteritems():
        result[name] = field_to_dict(field)
    return json.dumps(result)

def field_to_dict(field):
    return {
        "type": field.__class__.__name__,
        "widget_type": field.widget.__class__.__name__,
        "hidden": field.widget.is_hidden,
        "required": field.widget.is_required,
        "label": field.label,
        "help_text": field.help_text,
        "min_length": field.min_length, # optional
        "max_length": field.max_length, # optional
        "initial_value": field.initial,
    }

If you also want to handle error messages server side, you should probably include that information in field_to_dict as well.

To render a form as html, just convert it to a string.

over 4 years ago · Santiago Trujillo Report

0

<script>
var data = $("#form_id").serialize()
$.ajax({
  type: "POST",
  url: url,
  data: data,
  success: success,
  dataType: dataType
});
</script>

I guess... maybe... its really not clear what you are asking

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!