Home > Software design >  django if user.is_authenticated not working but login user
django if user.is_authenticated not working but login user

Time:05-09

my problem is that the login operation is done correctly and the session is set up correctly, but user.is_authenticated does not work. The point is that it works when I log in through the admin, but not like this, what can I do to fix it?

session set true image

my code :

views.py

class LoginView(View):
    def get(self, request):
        login_form = LoginForm()
        context = {
            'login_form': login_form
        }
        return render(request, 'account/login.html', context)

    def post(self, request: HttpRequest):
        login_form = LoginForm(request.POST)
        if login_form.is_valid():
            user_email = login_form.cleaned_data.get('email')
            user_password = login_form.cleaned_data.get('password')
            user: User = User.objects.filter(email__iexact=user_email).first()
            if user is not None:
                check_password = user.check_password(user_password)
                if check_password:
                    login(request, user)
                    return redirect('home')
                else:
                    login_form.add_error('password', 'password is not correct !')
            else:
                login_form.add_error('email', 'email dose not exists !')
        context = {
            'login_form': login_form
        }
        return render(request, 'account/login.html', context)

header_component.html

<div >
    <ul >
        {% for link in links %}
            <li ><a href="{{ link.url }}">{{ link.name|capfirst }}</a></li>
        {% endfor %}
        {% if user.is_authenticated %}
            <li ><a href="#">welcome</a></li>
            <li ><a href="{% url 'logout' %}">Log out</a></li>
        {% else %}
            <li ><a href="{% url 'login' %}">Login</a></li>
            <li ><a href="{% url 'register' %}">Register</a></li>
        {% endif %}
    </ul>
</div>

CodePudding user response:

{% if user.is_authenticated %} is deprecated.

Use this instead:

{% if request.user.is_authenticated %}
    # Do something for authenticated users.
    ...
{% else %}
    # Do something for anonymous users.
    ...

According to documentation:

Django uses sessions and middleware to hook the authentication system into request objects. These provide a request.user attribute on every request which represents the current user. If the current user has not logged in, this attribute will be set to an instance of AnonymousUser, otherwise it will be an instance of User. You can tell them apart with is_authenticated as shown above.

Edit: Add documentation explanation.

CodePudding user response:

I am writing this for friends who have this problem

The solution to this issue is that the user must be is_active equal to True to perform the login operation, otherwise the section

request.user.is_authenticated

Otherwise it does not work and does not enter the user

  • Related