0

Using django-allauth, how do I obtain form errors via AJAX login?

I am using this view with my /signup/ url:

from allauth.account.views import SignupView

Conveniently, it accepts my Ajax call:

var signupForm = function(token) {
   $.ajax({ url:"/signup/",
            type:"POST",
            data: {
                "email": $('#user').val(),
                "password1": $('#pass1').val(),
                "password2": $('#pass2').val()
            },
           success: function (){
                location.href = '/';
                window.location.reload(true);
           },
            error:function (er) {
                alert("signup failure...",er);
            }
        });
};

Inconveniently, if there is a problem with the form, it returns:

error 400: Bad Request

This gives me no idea why a login has failed. I would like to be able to convey to the user what happened. Was it a problem with her password? Was her email address already registered?

Can I accomplish this with all-auth, or do I need to write my own validation from javascript?

Adam Starrh
  • 6,428
  • 8
  • 50
  • 89

2 Answers2

3

I could not find a supported solution, so I hacked it:

from django.http import JsonResponse
from allauth.account.views import SignupView, _ajax_response

class AjaxSignUpView(SignupView):
    def post(self, request, *args, **kwargs):
        form_class = self.get_form_class()
        form = self.get_form(form_class)
        if form.is_valid():
            response = self.form_valid(form)
            return _ajax_response(
                self.request, response, form=form, data=self._get_ajax_data_if())
        else:
            return JsonResponse({'errors': form.errors})
Adam Starrh
  • 6,428
  • 8
  • 50
  • 89
  • Please, I just found your post it seems correct except that now I want to manage as you login and registration via a modal (Bootstrap) ajax and also have control over some context variables . Your answer suits me but only it is incomplete and above all you do not say how we could do it for Login view. Could you post for me a more complete answer for registration and login via ajax (for Django allauth of course)? Please... – Mbambadev Jan 24 '18 at 15:36
  • What other information are you looking for? You can use the exact same code for `LoginView`. This will send the errors back in a json object to the Ajax routine. – Adam Starrh Mar 02 '18 at 05:01
  • good luck uncle! thanks – enes islam Apr 23 '22 at 06:36
0

The issue with your code is that it is missing the csrfmiddlewaretoken. You should include it in the form (with the tag {% csrf_token %}) and then add it to your AJAX request like this:

$.ajax({ url:"/signup/",
        type:"POST",
        data: {
            "email": $('#user').val(),
            "password1": $('#pass1').val(),
            "password2": $('#pass2').val(),
            "csrfmiddlewaretoken": $('<selector for the csrf token input>').val()
        },
        ...

this item is required for all POST requests in django.

dethos
  • 3,336
  • 1
  • 15
  • 15