简体   繁体   English

如果 user.is_authenticated 未登录用户 django

[英]if user.is_authenticated not getting logged in user django

Entirely new to python and django framework.对 python 和 django 框架来说是全新的。

Trying to build a login/registration system.'试图建立一个登录/注册系统。

After registering, redirects to home but user is not authenticated (returns false always)注册后,重定向到主页但用户未通过身份验证(始终返回 false)

views.py视图.py

def register(request):
if request.method == 'POST':
    form = SignUpForm(request.POST)
    if form.is_valid():
        form.save()
        username = form.cleaned_data.get('username')
        fullname = form.cleaned_data.get('fullname')
        password = form.cleaned_data.get('password1')
        user = authenticate(username=username, fullname=fullname, password=password)
        messages.success(request, f'Welcome to blaza {fullname}')
        if user is not None:
            login(request, user)
        return redirect('home')
else:
    form = SignUpForm()

return render(request, 'accounts/signup.html', {'form': form})

home.html主页.html

{% extends 'base.html' %}
{% block title %} Home {% endblock %}
{% block content %}
{% if user.is_authenticated %}
Welcome, {{user.username}}!
<a href="">Logout</a>
{% else %}
<P>You are not logged in</P>
<p><a href="">Login here</a></p>
{% endif %}  
{% endblock %}

It returns false always.它总是返回假。 "You are not logged". “您没有登录”。

I have tried {% if request.user.is_authenticated %} still not working.我试过{% if request.user.is_authenticated %}仍然无法正常工作。

authenticate() with only username and password仅使用用户名和密码进行身份验证()

user = authenticate(request,username=username, password=password)

is_authenticated is a method not a property. is_authenticated是一种方法而不是属性。 I think you forgot the parenthesis.我想你忘记了括号。 Try this:尝试这个:

{% if request.user.is_authenticated() %}

Try what arjun suggested, but without request as a parameter尝试 arjun 的建议,但没有请求作为参数

Have you saved the user after signing up?您注册后是否保存了用户?

myuser.save() myuser.save()

I have attached the code which worked for me.我附上了对我有用的代码。

def signup(request):
if request.method == 'POST':

    username = request.POST['username']
    name = request.POST['name']
    email = request.POST['email']
    pass1 = request.POST['pass1']
    pass2 = request.POST['pass2']
    myuser = User.objects.create_user(username, email)
    myuser.set_password(pass1)
    myuser.us_name = name
    myuser.save()
    messages.success(request, "Your account has been sucessfully created. ")
    return redirect('signin')
return render(request, "authentication/signup.html")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Django如果user.is_authenticated不起作用 - Django if user.is_authenticated not working Django allauth未检测到user.is_authenticated - Django allauth does not detect user.is_authenticated 如果Django使用user.is_authenticated,则模板无法正确呈现 - Template not rendering properly with if user.is_authenticated for Django 为什么 user.is_authenticated 不起作用? django, LoginRequiredMixin - Why user.is_authenticated not working? django, LoginRequiredMixin 如何检查用户是否登录(如何正确使用user.is_authenticated)? - How to check if a user is logged in (how to properly use user.is_authenticated)? Django最好在views.py或模板中检查user.is_authenticated吗? - Django is it better to check user.is_authenticated in views.py or in template? 为什么 django 针对这种情况返回模板错误 {% if user.is_authenticated %}? - why django returns me a template Error for this case {% if user.is_authenticated %}? Django user.is_authenticated 不同视图返回不同结果 - Django user.is_authenticated returns different results in different views 已验证但user.is_authenticated仍然为false - Authenticated but user.is_authenticated remains false {% if user.is_authenticated %} 仅针对特定用户 - {% if user.is_authenticated %} for specific user only
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM