User Authentication Based Django App
  • Python Django
  • Basic Python Virtual Environment Setup
  • Project Set Up
  • Model-View-Templates Implementation
  • Model & Django Admin Area
  • View
    • Types of Views in Django
    • Project Todo Views
  • Django Templates
  • Todo App
    • DEMO: Workflow of Todo App
    • Base Template for Todo App
    • Registration Module
    • Login Module
    • Linking Login and Registration page
    • Task List User Authentication
    • Task Reordering
    • Logout Module
    • Task Create Module
    • Task Update
    • Task Delete
Powered by GitBook
On this page
  • 1. Create template for Registration module.
  • 2. Create view function for registration module
  • 3. Mapping URLs to Register view.
  • 4. Run the development server to check the functionality:
  1. Todo App

Registration Module

Steps to Develop a Registration module.

1. Create template for Registration module.

templates/todo/register.html

{% extends 'todo/main.html' %}
{% block content %}

<div class="header-bar">
    <h1>Register</h1>
</div>

<div class="card-body">
    <form method="POST">
        {% csrf_token %}
        <label>{{form.username.label}}</label>
        {{form.username}}

        <label>{{form.password1.label}}</label>
        {{form.password1}}

        <label>{{form.password2.label}}</label>
        {{form.password2}}

        {% if form.errors %}
        <ul class="errorlist">
            {% for field, error_list in form.errors.items %}
            {% for error in error_list %}
            <span class="aesthetic-span">{{ error }}</span>
            {% endfor %}
            {% endfor %}
        </ul>
        {% endif %}
        <input style="margin-top:10px ;" class="button" type="submit" value="Register">
    </form>
    <p>Already have an account? <a href="">Login</a></p>
</div>




{% endblock content %}

2. Create view function for registration module

Import Statement and view function.

from django.views.generic.edit import FormView
from django.contrib.auth.forms import UserCreationForm
from django.urls import reverse_lazy
from django.contrib.auth import login
class RegisterPage(FormView):
    template_name = 'todo/register.html'
    form_class = UserCreationForm
    redirect_authenticated_user = True
    success_url = reverse_lazy('home')
    
    def form_valid(self, form):
        user = form.save()
        if user is not None:
            login(self.request, user)
        return super(RegisterPage, self).form_valid(form)

Django code for a class-based view that handles user registration. Let me break down the code for you:

  1. RegisterPage is a class-based view that inherits from FormView. This suggests that it is designed to handle form submissions.

  2. template_name is set to 'todo/register.html', which indicates the HTML template to be rendered when this view is accessed.

  3. form_class is set to UserCreationForm, presumably a Django form class for user registration.

  4. redirect_authenticated_user is set to True, which means that if a user is already authenticated (logged in), they will be redirected to the URL specified in success_url without processing the form.

  5. success_url is set to reverse_lazy('home'), indicating the URL where the user should be redirected upon successful form submission.

  6. The form_valid method is overridden to handle the case when the form is valid. Inside this method:

    • form.save() is called to save the user object.

    • If the user object is not None, meaning it was successfully saved, the user is logged in using login(self.request, user).

    • Finally, the super() method is called to proceed with the default behavior of form_valid.

This code appears to be a part of a Django application for user registration, and it's using Django's built-in FormView and UserCreationForm to handle the registration process.

3. Mapping URLs to Register view.

Make sure you have the necessary URLs and other configurations in place to use this custom Register view. For example, in your urls.py file, you might have something like:

from django.urls import path

from .views import Home, CustomLoginView, RegisterPage

urlpatterns = [
    path('register/', RegisterPage.as_view(), name='register'),
    # Add other URL patterns as needed
]

4. Run the development server to check the functionality:

Start the development server if it's not running already:

python manage.py runserver

Access the interface by visiting http://localhost:8000/register in your web browser.

Output:

PreviousBase Template for Todo AppNextLogin Module

Last updated 1 year ago