# Registration Module

&#x20;Steps to Develop a Registration module.

## 1. Create template for Registration module.

`templates/todo/register.html`

```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.

```python
from django.views.generic.edit import FormView
```

```python
from django.contrib.auth.forms import UserCreationForm
```

```python
from django.urls import reverse_lazy
```

```python
from django.contrib.auth import login
```

```python
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:

```python
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:

<figure><img src="/files/OjuhfwtJISzMmd6sUrRk" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://sathyakumars-kb.gitbook.io/user-authentication-based-django-app/todo-app/registration-module.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
