user login in django code example

Example 1: django logout user

def logout_request(request):
      logout(request)
      return redirect("")

Example 2: django import user

from django.contrib.auth.models import User

Example 3: django login

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.
        ...

Example 4: loginrequiredmixin

from django.contrib.auth.mixins import LoginRequiredMixin

LOGIN_URL = 'your_url'

Example 5: import login required

from django.contrib.auth.decorators import login_required

Example 6: create login user django command

py manage.py  createsuperuser 
1=enter your userName
2=enter your email
3=enter your password
4=enter your password again 
5=for create user enter y (yes)