تغيير اللغه الي العربية

My Blog


All posts

Use Django's Authentication App part 4.

we have created profile home and profile edit, reset and change password, login, logout.

Now we will create the register view.

if you didn't read part 1 , part 2  and part 3 you should read them first.

First we will create a RegisterForm, we will use the UserCreationForm we used before, we can also use our CustomUserCreationForm that we already created, but we can create new form to add more fields.


# users/forms.py
from django import forms
from django.contrib.auth import authenticate, get_user_model
from django.contrib.auth.forms import (
AuthenticationForm,
UserChangeForm,
UserCreationForm,
)

User = get_user_model()

...
...
...

class RegisterForm(UserCreationForm):

def clean_email(self):
email = self.cleaned_data["email"]
if email:
match = User.objects.filter(email=email)
if match:
raise forms.ValidationError("This email address is already in use")
return email

class Meta:
model = User
fields = ('username', 'first_name', 'last_name', 'email')

Like we did before in the admin forms we checked the email if it exists.

You can read the post about checking email here



Then we will create our view, we will update our views.py like so.

# users/views.py
from django.contrib.auth import authenticate, login # New
from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.messages.views import SuccessMessageMixin
from django.urls import reverse_lazy
from django.views.generic import CreateView, DetailView, UpdateView # New

from .forms import RegisterForm, UserDetailChangeForm # New


...
...
...

class RegisterView(SuccessMessageMixin, CreateView):
template_name = 'registration/register.html'
success_url = reverse_lazy("users:user_profile")
form_class = RegisterForm
success_message = "Your profile was created successfully"

def form_valid(self, form):
valid = super(RegisterView, self).form_valid(form)
username, password = form.cleaned_data.get('username'), form.cleaned_data.get('password1')
new_user = authenticate(username=username, password=password)
login(self.request, new_user)
return valid


Then add the view to our urls.py

# users/urls.py

from django.contrib.auth import views as django_views
from django.urls import path

from . import views
from .forms import CustomLoginForm

app_name = "users"


urlpatterns = [
path("profile/", views.UserHomeView.as_view(), name="user_profile"),
path("details/", views.UserUpdateView.as_view(), name="account_update"),
path('register/', views.RegisterView.as_view(), name='register'), # New
path("logout/", django_views.LogoutView.as_view(), name="logout"),
path(
"login/",
django_views.LoginView.as_view(form_class=CustomLoginForm),
name="login",
),
]

Last thing is to create our register.html template

{# registration/register.html #}
{% extends 'base.html' %}
{% load static %}
{% block title %} | Register {% endblock %}

{% block content %}

<div>
{% if user.is_authenticated %}
<div>
<p>You are logged in</p>
<a href="/">Home Page</a>
</div>
{% else %}
<div>
<h2>Register</h2>
<div>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Register</button>
<p>Already registered?</p>
<a href="{% url 'users:login' %}">Login</a>
</form>
</div>
</div>
{% endif %}
</div>

{% endblock %}



And we are done! this is the last part of Django's auth app, we covered the most important things that we can do using it.

I hope the tutorial was simple.
Feel free to reach out if you have any questions.

Join to discord Server Here - Django Learn Together.

Last Updated: 1 month, 1 week ago
Views: 360