[Fixed]-Django – Ajax modal login/registration

7👍

In addition to Maddog’s answer you need some javascript to submit the form back to the URL that rendered the form. Using jquery it could be something like:

$('form').submit(function(e){
        e.preventDefault();
        var form = $(e.target);

        $.ajax({
            url: '{% url YOUR_REGISTRATION_URL %}',
            type: 'post',
            data: account_form.serialize() + '&' + form.serialize(),
            error: function(xhr, ajaxOptions, thrownError){ alert(thrownError); },
            success: function(){}
        })
 })

You don’t need to do it with a form submit element, you could use any element with $().click(), of course.

2👍

Something like this?

def ajax_registration(request):
    login_form, registration_form = False, False
    if request.method == "POST":
        if "email" in request.POST: # some condition to distinguish between login and registration form
            login_form = AuthenticationForm(request.POST)
            if login_form.is_valid():
                # log in
        else:
            registration_form = RegistrationForm(request.POST)
            if registration_form.is_valid():
                # register

    obj = {
        'login_form': login_form if login_form else AuthenticationForm(),
        'registration_form': registration_form if registration_form else RegistrationForm(),
    }
    return render(request, 'common/ajax_registration.html', obj)
👤Maddog

Leave a comment