[Answer]-Django building the login interface

1👍

Given the use-case outlined in comments, this is the approach I would take. Each form would contain a hidden field with a name attribute of action.

from django.contrib.auth import authenticate, login
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.shortcuts import render

def home(request):
    login_form = LoginForm(request.POST or None)
    registration_form = RegistrationForm(request.POST or None)

    if request.method == 'POST':
        action = request.POST.get('action')
        if action == 'login':
            if login_form.is_valid():
                user = authenticate(email=login_form.cleaned_data.get('email'),
                    password=login_form.cleaned_data.get('password'))
                login(request, user)
                return HttpResponseRedirect(reverse('some_url'))
        else:
            if registration_form.is_valid():
                registration_form.save()
                # do whatever else
    return render('some-template.html', {'login_form': login_form,
        'registration_form': registration_form})

Leave a comment