I'm a newbie to Django and Python. I'm learning with the Tango with Django Tutorial and I want to add some features to the tutorial login form. My issue is that I can't get the form errors to show when the login forms is taking the next parameter to redirect users after login. What is the best way to achieve this?
forms.py
from django import forms
from rango.models import Page, Category, UserProfile
from django.contrib.auth.models import User
class UserLoginForm(forms.Form):
username = forms.CharField(help_text="Please enter a username.", required=True)
password = forms.CharField(widget=forms.PasswordInput(), help_text="Please enter a password.", required=True)
class Meta:
model = User
fields = ('username', 'password')
views.py
from django.template import RequestContext
from django.shortcuts import render_to_response
from rango.models import Category, Page
from rango.forms import CategoryForm, PageForm, UserLoginForm, UserForm, UserProfileForm
from django.contrib.auth import authenticate, login, logout
from django.http import HttpResponseRedirect, HttpResponse
from django.contrib.auth.decorators import login_required
def user_login(request):
context = RequestContext(request)
NEXT = ""
if 'next' in request.GET:
NEXT = request.GET['next']
if request.method == 'POST':
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
user_login_form=UserLoginForm(request.POST)
errors=user_login_form.errors
if user_login_form.is_valid():
if user:
if user.is_active:
login(request, user)
if request.POST['next']:
return HttpResponseRedirect(request.POST['next'])
else:
return HttpResponseRedirect('/rango/')
else:
return HttpResponse("Your Rango account is disabled.")
else:
return HttpResponse ("Invalid login")
else:
if request.POST['next']:
return HttpResponseRedirect(request.POST['next'])
else:
print errors
else:
user_login_form=UserLoginForm()
return render_to_response('rango/login.html', {'NEXT': NEXT, 'user_login_form': user_login_form}, context)
{% if user_login_form.errors %} <p style="color:red">The login was unsuccessfull. Correct the errors in the form below!</p> {% endif %} <form class="form-signin" id="login_form" method="post" action="/rango/login/"> {% csrf_token %} {{ user_login_form.as_p }} <input type="hidden" name="next" value="{{ NEXT }}" /> <input type="submit" value="submit" /> </form>