why is request.user.is_authenticated printing out false even when user is logged in and rendering template

huangapple go评论59阅读模式
英文:

why is request.user.is_authenticated printing out false even when user is logged in and rendering template

问题

I am fairly new to Django and I use Django 4.2. I am building a website and I have run into this error. I have created my login function like this

def loginUsers(request):
    context = {}
    if request.method == 'POST':
        email = request.POST['email']
        upass = request.POST['password']

        user = authenticate(request, email=email, password=upass)
        if user is not None:
            login(request, user)
            messages.success(request, 'Login successful')
            time.sleep(2)
            return redirect(reverse('index'))
        else:
            messages.error(request, 'Invalid user')
            return redirect(reverse('login'))

    return render(request, 'auth_pages/login.html', context)

But my logout function is not in this contact app views.py. It is in my index app views.py because the logout button in my template is in the navbar. Here it is:

def loadIndexPage(request):
    context = {}
    print(request.user.is_authenticated)
    return render(request, 'pages/index.html', context)


def logoutUser(request):
    if request.method == 'POST':
        logout(request)
        messages.success(request, 'You are logged out')
        return redirect(reverse('login'))

The problem

The problem is that the logout function is not logging out the user, it instead, is creating a csrftokenmiddleware in the browser search bar and when I print the request.user.is_authenticated it prints false. In the template:

{% if request.user.is_authenticated %}
    <p>Hey there user</p>
{% else %}
    <p>Not working</p>
{% endif %}

This is not working. It is only showing Not working.

I have tried to add a @login_required to the index function, but it kept redirecting to a page that I never created or added in the urls.py of the project.

I have gone through the documentation and did everything as stated but yet the issues persist.

I have tried to print out the request.user.is_authenticated and it is still printing false, even though the user is authenticated in the login function.

I checked the database and the column, is_authenticated is still FALSE.

I have two questions:

  1. Is anything wrong with my code?
  2. Should I have set the column is_authenticated to True?
英文:

I am fairly new to Django and I use Django 4.2. I am building a website and I have run into this error. I have created my login function like this

def loginUsers(request):
    context = {}
    if request.method == &#39;POST&#39;:
        email = request.POST[&#39;email&#39;]
        upass = request.POST[&#39;password&#39;]

        user = authenticate(request, email=email, password=upass)
        if user is not None:
                    login(request, user)
                    messages.success(request, &#39;Login successful&#39;)
                    time.sleep(2)
                    return redirect(reverse(&#39;index&#39;))  
        else: 
            messages.error(request, &#39;Invalid user&#39;)
            return redirect(reverse(&#39;login&#39;))
        
    return render(request, &#39;auth_pages/login.html&#39;, context)

But my logout function is not in this contact app views.py. It is in my index app views.py because the logout button in my template is in the navbar. Here it is:

def loadIndexPage(request):
    context = {}
    print(request.user.is_authenticated)
    return render(request, &#39;pages/index.html&#39;, context)


def logoutUser(request):
    if request.method == &#39;POST&#39;:
        logout(request)
        messages.success(request, &#39;You are logged out&#39;)
        return redirect(reverse(&#39;login&#39;))

The problem

The problem is that the logout function is not logging out the user, it instead, is creating a csrftokenmiddleware in the browser search bar and when I print the
request.user.is_authenticated it prints false. In the template:

{% if request.user.is_authenticated %}
                    &lt;p&gt;Hey there user&lt;/p&gt;
                
                {% else %}
                    &lt;p&gt;Not working&lt;/p&gt;
{% endif %}

This is not working. It is only showing Not working.

I have tried to add a @login_required to the index function, but it kept redirecting to a page that I never created or added in the urls.py of the project.

I have gone through the documentation and did everything as stated but yet the issues persist.

I have tried to print out the request.user.is_authenticated and it is still printing false, even though the user is authenticated in the login function.

I checked the database and the column, is_authenticated is still FALSE.

I have two questions:

  1. Is anything wrong with my code?
  2. Should I have set the column is_authenticated to True?

答案1

得分: 1

你可能发出了一个GET请求,表单应该是:

<form method="POST" action="{% url 'logout' %}">
    {% csrf_token %}
    <button type="submit">注销</button>
</form>

可能缺少method="POST",所以它发出了GET请求。登录/注销应该通过POST请求进行,因为这会改变状态。

我检查了数据库和列,is_authenticated 仍然是FALSE。

通常,User 模型具有名为 is_authenticated 的列。这不是数据库列,对于所有的 User 来说都是真的。如果用户没有登录,request.User 将返回 AnonymousUser 对象,该对象的 is_authenticated 返回 False

英文:

You likely made a GET request, the form should be:

<pre><code>&lt;form <strong>method=&quot;POST&quot;</strong> action=&quot;{% url 'logout' %}&quot;&gt;
{% csrf_token %}
&lt;button type=&quot;submit&quot;&gt;logout&lt;/button&gt;
&lt;/form&gt;</code></pre>

Likely the method=&quot;POST&quot; is lacking, so it makes a GET request. Logging in/out should be done through a POST request, since this changes the state.

> I checked the database and the column, is_authenticated is still FALSE.

Normally the User model does not have a column named is_authenticated. This is not a database column, this is simply true for all Users. If the user is not logged in, request.User will return the AnonymousUser object, which has is_authenticated returning False.

huangapple
  • 本文由 发表于 2023年4月17日 04:00:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76030046.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定