Get Started With Django User Management
by:
blow post content copied from Real Python
click here to view original post
Django user management allows you to integrate user authentication and management into your web applications. By using Django, you can leverage the framework’s built-in authentication system to manage user registration, login, and logout. With just a few additional templates, you can enable users to reset and change their passwords independently.
This tutorial guides you through setting up a basic user management system with Django that you can extend later. You’ll learn how to create a dashboard, implement user registration, and connect authentication URLs, as well as customize templates for login, logout, and password management.
By the end of this tutorial, you’ll understand that:
- Django’s user authentication is a built-in authentication system that comes with pre-configured URLs and views.
- Authentication verifies user identity, while authorization determines user permissions within Django.
- Registering as a user in Django requires setting up views, templates, and URLs.
- Creating a login system in Django involves built-in authentication views and creating custom templates.
- Resetting passwords in Django involves configuring email backends for sending reset links.
This tutorial focuses on user authentication and user management. If you want to learn more about permissions and groups, then you can check out the tutorial about managing users in Django’s admin site.
Get Your Code: Click here to download the free sample code you’ll use to set up a basic user management system with Django .
Start With the Basics
For bigger projects, you may consider creating a custom user model. In this tutorial, you’ll be using Django’s built-in user model. This is a great place to start to familiarize yourself with user authentication in general.
In this section of the tutorial, you’ll first create a small Django project with a users
app. Then, you’ll make some adjustments to Django’s password validator to make your development more convenient. Finally, you’ll create an admin user to verify your setup.
Set Up the Django Project
It’s a good idea to use a virtual environment when working with Python projects. That way, you can always be sure that the python
command points to the right version of Python and that the modules required by your project have the correct versions. To read more about creating virtual environments, check out Python Virtual Environments: A Primer.
Select your operating system below and use your platform-specific command to set up a virtual environment:
With the above commands, you create and activate a virtual environment named venv
by using Python’s built-in venv
module. The parenthesized (venv)
in front of the prompt indicate that you’ve successfully activated the virtual environment.
Now that the environment is ready, you can install Django, start a new project, and create an application to store all your user management code:
(venv) $ python -m pip install Django
(venv) $ django-admin startproject user_auth_intro
(venv) $ cd user_auth_intro
(venv) $ python manage.py startapp users
In this example, you name your project user_auth_intro
and your application users
. To include the users
app in your Django project, you need to add a reference to the app’s configuration class at the beginning of the INSTALLED_APPS
list in settings.py
:
user_auth_intro/settings.py
1# ...
2
3INSTALLED_APPS = [
4 "users.apps.UsersConfig",
5 "django.contrib.admin",
6 "django.contrib.auth",
7 "django.contrib.contenttypes",
8 "django.contrib.sessions",
9 "django.contrib.messages",
10 "django.contrib.staticfiles",
11]
12
13# ...
By adding users.apps.UsersConfig
, you let Django know that the users
app you just created exists. If you have a look at the INSTALLED_APPS
list, then you’ll spot Django’s default authentication system on line 6. In django.contrib.auth
, Django stores the core of its authentication framework and the default models that you’ll build on later.
Next, apply the migrations and run the Django development server:
(venv) $ python manage.py migrate
(venv) $ python manage.py runserver
These commands create all default models in the database and start the Django development server.
Deactivate the Password Validator
By default, Django enforces strong passwords to make user accounts less prone to attacks. Since you’ll need to change passwords often throughout this tutorial, figuring out a strong password each time would be inconvenient.
Read the full article at https://realpython.com/django-user-management/ »
[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]
December 18, 2024 at 07:30PM
Click here for more details...
=============================
The original post is available in Real Python by
this post has been published as it is through automation. Automation script brings all the top bloggers post under a single umbrella.
The purpose of this blog, Follow the top Salesforce bloggers and collect all blogs in a single place through automation.
============================
Post a Comment