django user model 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: django user fields

# Django User Model Fields
username
first_name
last_name
email
password
groups
user_permissions
is_staff
is_active
is_superuser
last_login
date_joined

Example 4: how to get user id django

def sample_view(request):
    current_user = request.user
    print current_user.id

Example 5: django create user

from django.contrib.auth.models import User
user = User.objects.create_user(value['USERNAME'], value['EMAIL'], value['PASSWORD'])
user.first_name = value['NAME']
user.last_name = value['SURNAME']
user.save()

Example 6: how to get user id django

if request.user.is_authenticated:
    # Do something for authenticated users.
else:
    # Do something for anonymous users.