gpt4 book ai didi

html - 当用户使用 django 2.2 登录时将登录页面重定向到主页

转载 作者:行者123 更新时间:2023-11-28 02:22:02 25 4
gpt4 key购买 nike

我想在用户已经登录并尝试再次访问登录页面时将用户重定向到主页。我正在使用 Django 2.2。我尝试使用 user.is_authenticated 但它没有用

{% block content %}
{% if user.is_authenticated %}
{% block chess_page %}
{% endblock %}

{% else %}
<div id="logo">
<img src="../../static/img/chess-logo.jpg" name="logo-pic">
<h2>Chess</h2>
</div>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Login</button>
</form>
{% endif %}
{% endblock %}

chess_page 是我要重定向到的主页

最佳答案

不是在模板中检查 user.is_authenticated,而是在用于呈现登录页面的 View 中检查它,在登录 View 中检查 request.user.is_authenticated 并根据 is_authenticated 条件重定向到主页,如下所示:

def login(request):
if request.user.is_authenticated:
# redirect to homepage
# "chess_page" is the name you gave while creating url
return HttpResponseRedirect(reverse("chess_page"))
else:
# render login page
return render(request, "login.html")

关于html - 当用户使用 django 2.2 登录时将登录页面重定向到主页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56916270/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com