我在短版问题:Django的login_required装饰并不重定向到前一页
我已经加入了login_required
装饰,以我的观点之一。如果我在执行此视图的浏览器中输入URL,则如果用户未通过身份验证,则浏览器会正确重定向到包含我的登录表单的URL。但是,浏览器永远不会重定向回到上一页,我不知道为什么这不起作用。我尝试了数百件事情。
我在长版的问题:
我有一个单一的应用程序一个Django项目,姑且称之为my_app
。我的项目的所有模板都在templates/my_app/
。我有一个名为main.html
的模板,其中包含多个表单,其中包括我的登录表单。通过额外的POST
参数form-type
,我检查哪些表单已提交。代码看起来是这样的:
def process_main_page_forms(request):
if request.method == 'POST':
if request.POST['form-type'] == u'login-form':
template_context = _log_user_in(request)
elif request.POST['form-type'] == u'registration-form':
template_context = _register_user(request)
elif request.POST['form-type'] == u'password-recovery-form':
template_context = _recover_password(request)
else:
template_context = {
'auth_form': AuthenticationForm(),
'registration_form': RegistrationForm(),
'password_recovery_form': EmailBaseForm()
}
return render(request, 'my_app/main.html', template_context)
功能_log_user_in()
看起来像这样:
def _log_user_in(request):
message = ''
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
else:
message = 'Your account has been disabled. ' \
'Please contact the administrator.'
else:
message = 'Your username and password didn\'t match. Please try again.'
template_context = {
'auth_form': AuthenticationForm(),
'registration_form': RegistrationForm(),
'password_recovery_form': EmailBaseForm(),
'message': message,
}
return template_context
我还包括在模板中,例如必要的<input>
元件登录表单是这样的:
<input type="hidden" name="form-type" value="login-form" />
<input type="hidden" name="next" value="{{ next }}" />
该视图的URL模式是:
url(r'^$', process_main_page_forms, name='main-page')
我的第二个观点呈现了身份验证的用户更改电子邮件地址和密码两种形式。它看起来像这样:
@login_required(login_url='/')
def change_user_credentials(request):
if request.method == 'POST':
if request.POST['form-type'] == u'change-email-form':
template_context = _change_email_address(request)
elif request.POST['form-type'] == u'change-password-form':
template_context = _change_password(request)
else:
template_context = {'change_email_form': ChangeEmailForm()}
return render(request, 'my_app/user.html', template_context)
这第二种观点的URL模式是:
url(r'^account/$', change_user_credentials, name='user-page')
每当我访问/account/
当我没有验证,我成功地重定向到包含主页登录表单。生成的URL为http://127.0.0.1:8000/?next=/account/
,其中包含必要的next
参数。但是,当我登录我的帐户时,我仍然在主页上。尽管我在登录表单中提供了必要的参数next
,但我从来没有将其重定向到用户页面。看来这个参数总是空的,但我不知道为什么。我的代码中也没有其他重定向调用。
你能帮我解决这个问题吗?非常感谢你提前。
附注:你似乎在以一种非常奇怪的方式使用表单;而不是直接从'request.POST'获得'username'和'password',在实例化一个表单时使用'request.POST',* ie *'form = AuthenticationForm(request.POST)'然后在'form.is_valid )'。这样你得到验证(和一些消毒),你根本不需要'form-type'。它还可以解决您的代码中的当前错误,即如果密码错误,则不会记住输入的用户名。 – supervacuo 2012-07-11 19:03:22
请参阅[使用表格](https://docs.djangoproject.com/en/1.4/topics/forms/)文档获取更多信息。 – supervacuo 2012-07-11 19:03:50