Mark As Completed Discussion

User Management

In this section, we shall set up user login, logout, and registration.

Let's begin with registration, we'll start with the registration form, we'll be using the user creation form from django.contrib.auth.forms

PYTHON
1# ./crud_app/forms.py
2...
3from django import forms
4from django.contrib.auth.models import User
5from django.contrib.auth.forms import UserCreationForm
6
7class UserRegisterForm(UserCreationForm):
8    email = forms.EmailField()
9    class Meta:
10        model = User
11        fields = ['username', 'email', 'password1', 'password2']
12...

Next, we create a view for registering new users

PYTHON
1# ./crud_app/views.py
2...
3def register(request):
4    if request.method == 'POST':
5        form = UserRegisterForm(request.POST)
6        if form.is_valid():
7            form.save()          
8            return redirect('home')
9    else:
10        form = UserRegisterForm()
11    return render(request, 'crud_app/register.html', {'form': form})

Then we create a template for it

SNIPPET
1<!-- ./crud_app/templates/crud_app/home.html -->
2{% extends 'base.html' %}
3{% load static %}
4
5{% block title %}Register{% endblock title %}
6{% block content %}
7
8<div class="col-lg-12 blog-post">
9<form action="" method="post">
10    {{form.as_p}}
11    {% csrf_token %}
12    <button type="submit" class="btn btn-secondary">Register</button>
13</form>
14</div>
15
16{% endblock  %}

And finally, we add the view function to a path in urls.py

PYTHON
1# ./crud_app/urls.py
2...
3urlpatterns = [
4    ...
5    path('user/register', views.register, name='register'),
6    ...
7]

Now we update the register button on base.html to add the register URL, clicking on the button will bring us here where we can create a new user.

User Management

For the user login and logout, Django provides views and URLs, all we need to do is create a template in the registration directory and call it login.html.

SNIPPET
1<!-- ./crud_app/templates/registeration/login.html -->
2{% extends 'base.html' %}
3{% load static %}
4
5{% block title %}Login{% endblock title %}
6{% block content %}
7
8<div class="col-lg-12 blog-post">
9<form action="" method="post">
10    {{form.as_p}}
11    {% csrf_token %}
12    <button type="submit" class="btn btn-secondary">Login</button>
13</form>
14</div>
15{% endblock  %}

Next, we add the URL in our urls.py file

PYTHON
1# ./crud_app/urls.py
2...
3urlpatterns = [
4    ...
5    path('user/', include('django.contrib.auth.urls')),
6]

This path will give us access to login and logout routes, so let's update our base template and add these routes to their respective buttons.

And that is it for login and log out.

Or is it, well no it is not because after a successful login or logout we get the following error.

User Management

We are getting this error because Django redirects to a profile route by default, to change this behavior, we need to add an entry in our settings file that will tell Django which path to redirect to after login and logout.

open the settings.py file and add the following at the end.

PYTHON
1# ./django_crud/settings.py
2...
3LOGIN_REDIRECT_URL = 'home'
4LOGOUT_REDIRECT_URL = 'home'

And with that, we have ourselves a fully working CRUD app with Django.

Access all course materials today

The rest of this tutorial's contents are only available for premium members. Please explore your options at the link below.

Returning members can login to stop seeing this.