Welcome to the Django Tutorial! Today, we'll learn how to create users in Django, a powerful Python web framework. By the end of this tutorial, you'll understand how to create, manage, and authenticate users in your Django projects. Let's dive in! π
Django provides an integrated user authentication system, making it easier for you to manage user accounts and their permissions. When we create a new Django project, it comes with a pre-built auth app that contains models for handling users and groups.
To get started, let's create a new Django project:
django-admin startproject myproject
cd myprojectNow, let's create a new app named accounts:
python manage.py startapp accountsDjango uses the AbstractUser model in its auth app, which includes fields for username, email, first name, last name, and password. We can create a custom user model by extending the AbstractUser in our accounts app.
Open accounts/models.py and modify the file as follows:
from django.contrib.auth.models import AbstractUser
class CustomUser(AbstractUser):
passNow, let's register our custom user model in our project settings:
Open myproject/settings.py and find the AUTH_USER_MODEL setting. Set it to our custom user model:
AUTH_USER_MODEL = 'accounts.CustomUser'To allow Django to manage our custom user model, we need to register it. Open accounts/admin.py and modify the file as follows:
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import Group, User
from .models import CustomUser
class CustomUserAdmin(UserAdmin):
model = CustomUser
admin.site.unregistered_model(User)
admin.site.unregistered_model(Group)
admin.site.register(CustomUser, CustomUserAdmin)Now, when you run the Django admin site, you should be able to create and manage users using our custom user model.
Let's create a function to create a new user programmatically. Open accounts/models.py and add the following code:
from django.contrib.auth import get_user_model
from django.contrib.auth.password_validation import validate_password
def create_user(username, email, password, **extra_fields):
user = get_user_model().objects.create_user(
username=username,
email=email,
password=password,
**extra_fields
)
user.save(using=None, keep_passhash=False)
user.set_password(password)
user.save()
return userHere, we create a create_user function that takes the username, email, and password as arguments, along with any additional fields you might want to set for the user.
Let's test our create_user function in the Django shell:
python manage.py shellNow you can create a new user:
from accounts.models import create_user
user = create_user('myusername', 'myemail@example.com', 'mypassword')
print(user)Congratulations! You now know how to create users in Django, including managing users programmatically. In the next tutorial, we'll learn how to create and manage user permissions. Keep learning, and happy coding! π
What is the name of the pre-built user authentication app in Django?