How fix redirect after registration? Django

today I faced such a problem that after registration the form does not go anywhere. The catch is that the form does not send a request to the database, but I can create a user through the Django admin panel.

views.py

def registration(request):
    if request.method == 'POST':
        form = UserRegistrationForm(data=request.POST)
        if form.is_valid():
            form.save()
            user = form.instance
            auth.login(request, user)
            messages.success(
                request, f'{user.username}, Successful Registration'
            )
            return HttpResponseRedirect(reverse('user:login'))
    else:
        form = UserRegistrationForm()
    return render(request, 'users/registration.html')

forms.py

from django import forms
from django.contrib.auth.forms import AuthenticationForm, \
    UserCreationForm, UserChangeForm
from .models import User


class UserLoginForm(AuthenticationForm):
    username = forms.CharField()
    password = forms.CharField()
    
    
    class Meta:
        model = User
        fields = ['username', 'password']
        

class UserRegistrationForm(UserCreationForm):
    class Meta:
        model = User
        fields = (
            'first_name',
            'last_name',
            'username',
            'email',
            'password1',
            'password2',
        )
        
        
        first_name = forms.CharField()
        last_name = forms.CharField()
        username = forms.CharField()
        email = forms.CharField()
        password1 = forms.CharField()
        password2 = forms.CharField()
        

class ProfileForm(UserChangeForm):
    class Meta:
        model = User
        fields = (
            'image',
            'first_name',
            'last_name',
            'username',
            'email',
        )
    
    
        image = forms.ImageField(required=False)
        first_name = forms.CharField()
        last_name = forms.CharField()
        username = forms.CharField()
        email = forms.CharField()

registration.html

{% extends 'main/base.html' %}
{% load static %}

{% block title %}Registration{% endblock title %}

{% block content %}
    <section class="login-reg d-flex">
        <div class="login-title">
            <h2>Registration</h2>
            <form action="{% url "user:registration" %}" method="post" enctype="multipart/form-data">
                {% csrf_token %}
                <div class="row">
                    <div class="col-md-6 mb-3">
                        <label for="id_first_name" class="form-label form-stylereg">First Name</label>
                        <input type="text" class="form-control form-stylereg" id="id_first_name"
                        value="{% if form.first_name.value %}{{ form.first_name.value }}{% endif %}"
                        name="first_name" placeholder="Your First Name" required>
                    </div>
                    <div class="col-md-6 mb-3">
                        <label for="id_last_name" class="form-label form-stylereg">Last Name</label>
                        <input type="text" class="form-control form-stylereg" id="id_last_name"
                        value="{% if form.last_name.value %}{{ form.last_name.value }}{% endif %}"
                        name="last_name" placeholder="Your Last Name" required>
                    </div>
                    <div class="col-md-6 mb-3">
                        <label for="id_username" class="form-label form-stylereg">Username</label>
                        <input type="text" class="form-control form-stylereg" id="id_username"
                        value="{% if form.username.value %}{{ form.username.value }}{% endif %}"
                        name="username" placeholder="Your Username" required>
                    </div>
                    <div class="col-md-6 mb-3">
                        <label for="id_email" class="form-label form-stylereg">Email</label>
                        <input type="email" class="form-control form-stylereg" id="id_email"
                        value="{% if form.email.value %}{{ form.email.value }}{% endif %}"
                        name="email" placeholder="Your Email" required>
                    </div>
                    <div class="col-md-6 mb-3">
                        <label for="id_password1" class="form-label form-stylereg">Password</label>
                        <input type="password" class="form-control form-stylereg" id="id_password1"
                        value="{% if form.password1.value %}{{ form.password1.value }}{% endif %}"
                        name="password1" placeholder="Your Password" required>
                    </div>
                    <div class="col-md-6 mb-3">
                        <label for="id_password2" class="form-label form-stylereg">Confirm Password</label>
                        <input type="password" class="form-control form-stylereg" id="id_password2"
                        value="{% if form.password2.value %}{{ form.password2.value }}{% endif %}"
                        name="password2" placeholder="Your Password" required>
                    </div>
                </div>
                <button type="submit" class="login-btn form-style">Register</button>
            </form>
        </div>
    </section>

    {% if form.errors %}
        <div id="form-error">
            <p>errors list:</p>
            <ul>
                {% for field in register_form %}
                    <li>{{ field.errors|striptags }}</li>
                {% endfor %}
            </ul>
        </div>
    {% endif %}

{% endblock content %}

urls.py

from django.urls import path
from . import views


app_name = 'users'

urlpatterns = [
    path('login/', views.login, name='login'),
    path('registration/', views.registration, name='registration'),
    path('profile/', views.profile, name='profile'),
    path('logout/', views.logout, name='logout'),
]

I changed the registration form, but nothing worked, please help me!

Вернуться на верх