Django Tutorial: Creating Users 🎯

beginner
22 min

Django Tutorial: Creating Users 🎯

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! 🌊

Understanding User Authentication in Django πŸ“

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.

Setting Up the Project βœ…

To get started, let's create a new Django project:

bash
django-admin startproject myproject cd myproject

Now, let's create a new app named accounts:

bash
python manage.py startapp accounts

Creating the User Model πŸ’‘

Django 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:

python
from django.contrib.auth.models import AbstractUser class CustomUser(AbstractUser): pass

Now, 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:

python
AUTH_USER_MODEL = 'accounts.CustomUser'

Registering the User Model πŸ“

To allow Django to manage our custom user model, we need to register it. Open accounts/admin.py and modify the file as follows:

python
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.

Creating Users Programmatically πŸ’‘

Let's create a function to create a new user programmatically. Open accounts/models.py and add the following code:

python
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 user

Here, 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.

Creating Users in the Django Shell βœ…

Let's test our create_user function in the Django shell:

bash
python manage.py shell

Now you can create a new user:

python
from accounts.models import create_user user = create_user('myusername', 'myemail@example.com', 'mypassword') print(user)

Wrapping Up πŸ“

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! πŸš€

Quiz πŸ’‘

Quick Quiz
Question 1 of 1

What is the name of the pre-built user authentication app in Django?