django.contrib.auth.views.login returns Anonymous user

litwisha

I want to login my user after registration, but when call login(request, user) then request.user is still AnonymousUser.

def register(request):
    if request.method=='POST':
        form = RegistrationForm(request.POST)
        if form.is_valid():
            form.save()
            username = form.cleaned_data['username']
            pwd = form.cleaned_data['password1']
            user = authenticate(username=username, password=pwd)
            if user is not None and user.is_active:
                auth_views.login(request, user)
            return redirect('/')
    else:
        form = RegistrationForm()

    return render(request, 'registration.html', {'form' : form})

When calling login in my own login method, it works correct. But in

`register` view not:
def login(request):
    if request.method=="POST":
        form = AuthenticationForm(data=request.POST)
        if form.is_valid():
            username = form.cleaned_data['username']
            password = form.cleaned_data['password']
            user = authenticate(username=username, password=password)
            if user is not None and user.is_active:
                auth_views.login(request, user)
                return redirect(reverse('chat'))
            else:
                return redirect('/')
    else:
        form = AuthenticationForm()

    return render(request, 'login.html', {'form':form})
viktor.svirskyy

Try:

from django.contrib.auth import authenticate, login

def my_view(request):
    username = request.POST['username']
    password = request.POST['password']
    user = authenticate(username=username, password=password)
    if user is not None:
        if user.is_active:
            login(request, user)
            # Redirect to a success page.
        else:
            # Return a 'disabled account' error message
            ...
    else:
        # Return an 'invalid login' error message.
        ...

And check your session settings

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Django cannot import login from django.contrib.auth.views

Error "Reverse for 'django.contrib.auth.views.login' not found"

django.contrib.auth.login() function not returning any user as logged in

Django test client returns an anonymous user after login

Django request.user is anonymous in views without login_required decorator

How to override PasswordResetConfirmView? (django.contrib.auth.views)

Using App name with Django.contrib.auth views

Django auth registration views login redirect

Where is django.contrib.auth.User defined in Django source code

Django REST framework TokenAuthentication returns anonymous user

how to fixe django.contrib.auth.models.AnonymousUser to User

SecurityContextHolder returns correctly login user as "anonymous" in junit tests

Django auth: self.request.user is always Anonymous in viewset

"AttributeError: module 'django.contrib.auth.views' has no attribute 'password_reset' " error in urls.py

Python from django.contrib.auth.views import logout ImportError: cannot import name 'logout'

AttributeError: module 'django.contrib.auth.views' has no attribute 'add_comment_to_post'

Django, how to get a user by id, using the django.contrib.auth.models.User

How to use django-avatar with my own User model and not with django.contrib.auth.models.User

Cannot assign "<class 'django.contrib.auth.models.User'>": "Model.user" must be a "User" instance

Django API OneToOne relationship using Auth, User model, Views, Serializers

from django.contrib.auth.models import User vs defining an user ForeignKey as owner = models.ForeignKey('auth.User')

django.contrib.auth.models.User.DoesNotExist: User matching query does not exist

Should I be using the django.contrib.auth.models User model? Or is it better to create my own User model?

Django: ImportError: cannot import name 'get_user_model' from 'django.contrib.auth.models'

Q: Django ImproperlyConfigured - from django.contrib.auth.models import User

Django Rest Framework - Testing client.login doesn't login user, ret anonymous user

Render personalized data in template against django.contrib.auth.models.User

Django REST Framework and python-social-auth for registration/login user

Django user is still AnonymousUser after calling auth.login()