On Monday, December 28, 2015 at 11:11:13 AM UTC+5:30, Robin Lery wrote:
>
> I have a contact form through which users would be able to contact me. I 
> am using django with ajax, and it works fine if there's no error. I would 
> like to show the errors if there's any like it displays above the input 
> fields and not just the errors, but both the input and the errors. It does 
> however differentiate between the success and error result, as the ajax 
> request was successful. But I need to display the actual form errors. 
>
> I would like to display the errors like the below as it normally does 
> without ajax:
>
>
> ​
>
> How do I that? Your help will be very much appreciated. Thank you.
>
> views:
>
> def contact(request):
>     if request.is_ajax() and request.POST:
>         form = ContactForm(request.POST)
>         if form.is_valid():
>             new_contact = form.save()
>             data = {
>                 'result': 'success',
>                 'message': 'Message Sent.'
>             }
>             return JsonResponse(data)
>         else:
>             data = {
>                 'result': 'error',
>                 'message': 'Form invalid',
>                 'form': 'oops.'
>             }
>             return JsonResponse(data)
>     else:
>         form = ContactForm()
>         return render(request, 'public_contact.html', {
>             'form': form
>         })
>
> js:
>
> contact_right_form.find('#submit').on('click', function(event) {
>     event.preventDefault();
>     $.ajax({
>         type: contact_right_form.attr('method'),
>         url: '/contact/',
>         data: contact_right_form.serialize(),
>         dataType: 'json',
>         success: function(data) {
>             if ( data.result == 'success') {
>                 contact_right_message_sent.text(data.message);
>                 contact_right_message_sent.show();
>             }
>             else {
>                 contact_right_message_sent.text(data.message);
>                 contact_right_message_sent.show();
>             }
>         },
>         error: function() {
>             contact_right_message_sent.text('Sorry! Something went 
> wrong.')
>         }
>     });
> })
>


Can u please clarify what do you want to accomplish?? 

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/59c41277-3b9d-4390-b4f6-ef6fdfbfb8fc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to