django create user code example

Example 1: django login required decorator

from django.contrib.auth.decorators import login_required

@login_required
def my_view(request):
    ...

Example 2: django admin password reset

python manage.py changepassword <user_name>

Example 3: create super user in django

python3 manage.py createsuperuser

Example 4: django import user

from django.contrib.auth.models import User

Example 5: create django user command line

user@hostname$ python3 -m django shell
>>> import django.contrib.auth
>>> User = django.contrib.auth.get_user_model()
>>> user = User.objects.create_user('username', password='userpassword')
>>> user.is_superuser = False
>>> user.is_staff = False
>>> user.save()

Example 6: 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()