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

标签 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

最佳答案

如果我理解你的问题

为什么custom_logout

你可以直接调用 django logout 就像

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

关于当用户进入注册页面时,Django 自动注销用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12092675/

相关文章:

jquery - django-autocomplete-light : "The results could not be loaded"

django - 如何使用 `path` 或 `re_path` 进行捕获所有路由,以便 Django 将所有不匹配的请求传递到我的索引 View ?

python - 管理 Django 中的错误有困难 - NoReverseMatch at

python - 使用 Requests 和 Django 从脚本登录网页

django - user.is_authenticated 在 html 中有效,但在 View 中无效

Django:/logout 切换语言

python - Django 管理员 : What is the best way to add a custom list view?

python - Django Python 脚本不输出文件

python - Django 1.7 迁移不会重新创建删除的表,为什么?

regex - 使 Regex Django URL token 可选