我是Django和Python的新手。我正在学习使用Django Tutorial的Tango,我想在教程登录表单中添加一些功能。我的问题是,当登录表单在登录后使用下一个参数重定向用户时,我无法显示表单错误。实现这一目标的最佳方法是什么?
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)
答案 0 :(得分:1)
如果登录成功并且登录不成功,我猜你是否试图将用户重定向到网址,显示表单错误(如果我错了,请纠正我)。此网址位于' next'参数。
因为如果您遇到表单错误,用户就不会进行身份验证而您也不必重定向,对吧?
def user_login(request):
context = RequestContext(request)
errors = None
if 'next' in request.GET:
NEXT = request.GET['next']
if request.method == 'POST':
username = request.POST['username']
password = request.POST['password']
user_login_form = UserLoginForm(request.POST)
if user_login_form.is_valid():
user = authenticate(username=username, password=password)
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:
errors = user_login_form.errors
NEXT = request.POST['next']
user_login_form=UserLoginForm()
return render_to_response('rango/login.html', {'NEXT': NEXT,
'user_login_form': user_login_form,
'errors': errors}, context)
当errors
非空时,让模板显示表单错误。
更多强>
模板可能类似于
<p>{{ errors }}</p>
<form action="/contact/" method="post">
{{ user_login_form.as_p }}
<input type="submit" value="Submit" />
</form>
当然,这是一个裸露的骨架。阅读this文章,了解更好地展示表单和表单错误的方法。
答案 1 :(得分:0)
重定向,不含 next
参数。
def user_login(request):
...
if form.is_valid():
return HttpResponseRedirect('/user-profile/')
else:
# do something else
以上,/user-profile/
是您要在登录后将用户重定向到的页面的url
。