django get user code example
Example 1: how to know connected user in django
def sample_view(request):
current_user = request.user
print current_user.id
Example 2: django login required decorator
from django.contrib.auth.decorators import login_required
@login_required
def my_view(request):
...
Example 3: django import user
from django.contrib.auth.models import User
Example 4: django request user
def my_view(request):
if not request.user.is_authenticated:
return render(request, 'myapp/login_error.html')
Example 5: loginrequiredmixin
from django.contrib.auth.mixins import LoginRequiredMixin
LOGIN_URL = 'your_url'
Example 6: how to get user id django
def sample_view(request):
current_user = request.user
print current_user.id