这是我在 view.py 中的登录功能
def login_user(request):
form = LoginForm(request.POST or None )
if request.method == 'POST':
if form.is_valid():
username = form.cleaned_data['username']
password = form.cleaned_data['password']
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
# Redirect to a success page.
userprofile = UserProfile.objects.get(user = user)
request.session['userprofile'] = userprofile
request.session['familymembers'] = UserProfile.objects.filter(family = userprofile.family)
return redirect('/profile')
else:
# Return a 'disabled account' error message
messages.add_message(request, messages.ERROR, '.')
return redirect('/profile')
else:
messages.add_message(request, messages.ERROR, '.')
return render_to_response('registration/login.html',{'form':form},RequestContext(request))
return render_to_response('registration/login.html', {'form': form},RequestContext(request))
elif request.method == 'GET':
return render_to_response('registration/login.html',{'form':form},context_instance = RequestContext(request))
当表单is_valid()
失败时,相同的登录页面显示错误消息,现在当我刷新页面时,弹出一个确认框说重新提交表单确认,
我的问题是:是否可以避免这个确认框,我试过了
return redirect('/', {'form': form},RequestContext(request))
这将避免确认框,但有效的错误消息消失了。