Wednesday, July 1, 2020

Get Started With Django Part 2: Django User Management

If you finished the first part of this series, then you may already have a lot of ideas for your own Django applications. At some point, you might decide to extend them with user accounts. In this step-by-step tutorial, you’ll learn how to work with Django user management and add it to your program.

By the end of this tutorial, you’ll be able to:

  • Create an application where users can register, log in, and reset and change passwords on their own
  • Edit the default Django templates responsible for user management
  • Send password reset emails to actual email addresses
  • Authenticate using an external service

Let’s get started!

Set Up a Django Project

This tutorial uses Django 3.0 and Python 3.6. It focuses on user management, so you won’t use any advanced or responsive styling. It also doesn’t deal with groups and permissions, only with creating and managing user accounts.

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 correct versions. To read more about it, check out Python Virtual Environments: A Primer.

To set up a virtual environment on Linux and macOS, run these commands:

$ python3 -m venv venv
$ source venv/bin/activate
(venv) $ python -m pip install --upgrade pip
(venv) $ python -m pip install django

To activate a virtual environment on Windows, run this command:

C:\> venv\Scripts\activate

Now that the environment is ready, you can create a new project and an application to store all your user management code:

(venv) $ django-admin startproject awesome_website
(venv) $ cd awesome_website
(venv) $ python manage.py startapp users

In this example, your application is called users. Keep in mind that you need to install it by adding it to INSTALLED_APPS:

# awesome_website/settings.py

INSTALLED_APPS = [
    "users",
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
]

Next, apply the migrations and run the server:

(venv) $ python manage.py migrate
(venv) $ python manage.py runserver

This will create all user-related models in the database and start your application at http://localhost:8000/.

There’s one more thing you should do for this setup. By default, Django enforces strong passwords to make user accounts less prone to attacks. But you’re going to change passwords very often during the course of this tutorial, and figuring out a strong password each time would be very inconvenient.

You can solve this issue by disabling password validators in settings. Just comment them out, leaving an empty list:

# awesome_website/settings.py

AUTH_PASSWORD_VALIDATORS = [
    # {
    #     "NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
    # },
    # {
    #     "NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",
    # },
    # {
    #     "NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",
    # },
    # {
    #     "NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",
    # },
]

Now Django will allow you to set passwords like password or even pass, making your work with the user management system much easier. Just remember to enable the validators in your actual application!

For this tutorial, it would also be useful to have access to the admin panel so you can track newly created users and their passwords. Go ahead and create an admin user:

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 ]



from Real Python
read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...