Django在进入注册页面时自动注销用户

时间:2012-08-23 13:26:56

标签: django django-urls django-authentication django-registration django-login

提前致谢。

我在我的一个Django网站上遇到了一个问题。经过身份验证的用户可以访问注册页面。但客户提出这是一个问题。因此,我试图纠正该问题并最终得到以下解决方案。

这是一个很好的解决方案吗?或者我怎样才能做到好?

进程应该是这样的,当登录用户尝试访问注册页面时,他应该自动从网站注销,然后重定向到注册页面。

我的代码是

def user_signup(request, template_name='profiles/profile_register_form.html'):
if request.user.is_authenticated():
    return custom_logout(request, next_page = "/accounts/register/")


def custom_logout(request, next_page='/'):
try:
    language = request.session['django_language']
except:
    language = False
response = logout(request, next_page=next_page)
if language:
    request.session['django_language'] = language
return response

2 个答案:

答案 0 :(得分:4)

你的方法是正确的。它将保存当前的语言会话,并将执行您需要的确切过程

答案 1 :(得分:0)

如果我理解你的问题那么

为什么custom_logout

您可以直接调用django logout,如

if request.user.is_authenticated():
    logout(request)
    return HttpResponseRedirect('/login/')  # whatever you register page 
相关问题