Django form validation message not showing up in viewa in jquery ajax post

I created a django form and is shown on the html page from django views. On the page I need to send data from jquery ajax, and I did, if the data is valid, then my django valid will get True and complete the task and return the json value.

But in case the invalid data means that it will not display the corresponding html with django error messages such as submitting a regular form.

Is there a way to make the django validation message shown in the appropriate fields (such as regular mail validation in the form of django) using the jQuery ajax post method. Please advise everyone, Thnaks.

0
source share
2 answers

HTML- render_to_string, render_to_response, unicode

BeautifulSoup HTML, XHTML XML, HTML- :

        if form.is_valid():
            form.save()
            html = render_to_string("template.html",
            {'form':form},context_instance=RequestContext(request))            
            soup = BeautifulSoup(html)
            html_form = soup.findAll('form')            
            json = simplejson.dumps({'status':True,
                'html':unicode(html_form[0]),
                'umessage':js_translators.NotifyMessage.data_saved}, ensure_ascii=False) 
            return HttpResponse(json, mimetype="application/json")
        else:
            html = render_to_string("template.html",
            {'form':form},context_instance=RequestContext(request))            
            soup = BeautifulSoup(html)
            html_form = soup.findAll('form')            
            json = simplejson.dumps({'status':False,
                'html':unicode(html_form[0]),
                'umessage':js_translators.NotifyMessage.data_wrong}, ensure_ascii=False)
            return HttpResponse(json, mimetype="application/json")

, jQuery Ajax :

$('#myform').html(response.html);

django ajax

0

Source: https://habr.com/ru/post/1693448/


All Articles