Auto login

Posted under » Django on 5 Aug 2025

If you have an authenticated user you want to attach to the current session - this is done with a login() function.

from django.contrib.auth import authenticate, login

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

web security linux ubuntu python django git Raspberry apache mysql php drupal cake javascript css AWS data