[英]Django user authentication not working on every subpage
i have strange (and probable simple) problem with user authentication in Django.我在 Django 中遇到用户身份验证的奇怪(并且可能很简单)问题。 I added to base.html file this code:
我将此代码添加到 base.html 文件中:
{% if user.is_authenticated %}
<p>{{user.get_username}} is logged in</p>
<a href="{% url 'logout' %}">logout</a>
{% else %}
<p>You are not logged in!</p>
<a href="{% url 'login' %}">login</a></a>
{% endif %}
Then i added this line to every other template:然后我将此行添加到每个其他模板中:
{% extends "base.html" %}
{% 扩展 "base.html" %}
It looks good because every subpage display this lines of code, unfortunately sometimes is displaying code when user is authenticated and sometimes where he is not authenticated.For example when i switch between two subpages once i get information that user with his login is logged, on this second webpage i get information that i am not logged in and when i come back to that first subpage i am again authenticated...它看起来不错,因为每个子页面都显示这行代码,不幸的是,有时在用户通过身份验证时显示代码,有时在用户未通过身份验证时显示代码。例如,当我获得用户登录信息后,我在两个子页面之间切换这第二个网页我得到信息,我没有登录,当我回到第一个子页面时,我再次通过身份验证......
How could i change it?我怎么能改变它? What could be a problem?
有什么问题?
my views about login:我对登录的看法:
def login_user(request):
login_user = {}
login_user.update(csrf(request))
return render_to_response('login.html', login)
def auth_view(request):
username = request.POST.get('username', '')
password = request.POST.get('password', '')
user = auth.authenticate(username=username, password=password)
if user is not None and user.is_active:
auth.login(request, user)
return redirect('loggedin')
else:
return HttpResponseRedirect('invalidlogin')
def logged_in(request):
return render_to_response('loggedin.html',
{'user': request.user})
and my 'login' urls:和我的“登录”网址:
path('login', views.login, name='login'),
path('authorized', views.auth_view, name='authorized'),
path('loggedout', views.loggedout, name='loggedout'),
path('loggedin', views.loggedin, name='loggedin'),
path('invalidlogin', views.invalidlogin, name='invalidlogin'),
from django.contrib.auth.decorators import login_required
@login_required(login_url='/accounts/login/')
def my_view(request):
https://docs.djangoproject.com/en/3.1/topics/auth/default/#django.contrib.auth.decorators.login_required https://docs.djangoproject.com/en/3.1/topics/auth/default/#django.contrib.auth.decorators.login_required
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.