Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6616987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:39:00+00:00 2026-05-25T20:39:00+00:00

I’ve been finding my way around Django and jQuery. I’ve built a basic form

  • 0

I’ve been finding my way around Django and jQuery. I’ve built a basic form in Django. On clicking submit, I’m using jQuery to make an AJAX request to the sever to post my data. This bit seems to work fine and I’ve managed to save the data. Django returns a ValidationError when a form is invalid. Could anyone tell me how to return this set of error messages as a response to my AJAX request so I can easily iterate through it using JS and do whatever?

I found this snippet. Looking at the JS bit (processJson) you’ll see that he seems to get the error messages by extracting them from the response HTML. It seems kinda kludgy to me. Is this the best way to go about it?

My apologies for any vagueness.

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T20:39:00+00:00Added an answer on May 25, 2026 at 8:39 pm

    Wow, it’s been a year since I’ve seen this thread. Well, with the advent of Django 1.3 and the magical, undocumented class-based views, it’s become more easy to extent Django’s view related functionality. My project which makes heavy use of Django’s class-based generic CRUS views need AJAX and JSON functionality. I’ve added an example of how I’ve modified Django’s update view to support AJAX and return AJAX responses in the JSON format. Have a look:

    def errors_to_json(errors):
        """
        Convert a Form error list to JSON::
        """
        return dict(
                (k, map(unicode, v))
                for (k,v) in errors.iteritems()
            )
    
    class HybridUpdateView(UpdateView):
        """
        Custom update generic view that speaks JSON
        """
        def form_valid(self, form, *args, **kwargs):
            """
            The Form is valid
            """
            form.save()
    
            self.message = _("Validation passed. Form Saved.")
            self.data = None
            self.success = True
    
            payload = {'success': self.success, 'message': self.message, 'data':self.data}
    
            if self.request.is_ajax():
                return HttpResponse(json.dumps(payload),
                    content_type='application/json',
                )
            else:
                return super(HybridUpdateView, self).form_valid(
                    form, *args, **kwargs
                )
    
        def form_invalid(self, form, *args, **kwargs):
            """
            The Form is invalid
            """
            #form.save()
    
            self.message = _("Validation failed.")
            self.data = errors_to_json(form.errors)
            self.success = False
    
            payload = {'success': self.success, 'message': self.message, 'data':self.data}
    
            if self.request.is_ajax():
                return HttpResponse(json.dumps(payload),
                    content_type='application/json',
                )
            else:
                return super(HybridUpdateView, self).form_invalid(
                    form, *args, **kwargs
                )
    

    The response JSON contains three fields — message (which is a human readable message), data (which is this case would be the list of form errors) and success (which is either true or false, indicating whether the request was successful or not respectively.). This is very easy to handle in jQuery client-side. A sample response looks like:

    Content-Type: application/json
    
    {"message": "Validation failed.", "data": {"host": ["This field is required."]}, "success": false}
    

    This is just an example of how I serialized the form errors to JSON and implemented it in a class-based generic view but can be cannibalized to work with regular style views as well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm making a simple page using Google Maps API 3. My first. One marker
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have a text area in my form which accepts all possible characters from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.