if user.is_authenticated django code example
Example 1: django import user
from django.contrib.auth.models import User
Example 2: django request user
def my_view(request):
if not request.user.is_authenticated:
return render(request, 'myapp/login_error.html')
Example 3: from django.contrib.auth.decorators import authenticate, login
from django.contrib.auth import authenticate, login
Example 4: loginrequiredmixin
from django.contrib.auth.mixins import LoginRequiredMixin
LOGIN_URL = 'your_url'
Example 5: how to get user id django
def sample_view(request):
current_user = request.user
print current_user.id
Example 6: user login validation django
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)
...
else:
...