Sardiya
Sardiya

Reputation: 1

Registration system not working in Django

Hello, I have created a user registration system, but when I fill in the information on the registration page and click on registration, no user is created and only the same form is displayed on the page, please guide me.

my views
from django.shortcuts import render, redirect, get_object_or_404
from django.views import View
from .forms import UserRegistrationForm
from django.contrib.auth.models import User
from django.contrib import messages
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.auth import views as auth_views
from django.urls import reverse_lazy

class UserRegisterView(View):
form_class = UserRegistrationForm
template_name = 'accounts/signup.html'

    def dispatch(self, request, *args, **kwargs):
        if request.user.is_authenticated:
            return redirect('home:home')
        return super().dispatch(request, *args, **kwargs)
    
    def get(self, request):
        form = self.form_class()
        return render(request, self.template_name, {'form':form})
    
    def post(self, request):
        form = self.form_class(request.POST)
        if form.is_valid():
            cd = form.cleaned_data
            User.objects.create_user(cd['username'], cd['email'], cd['password1'])
            messages.success(request, 'you registered successfully', 'success')
            return redirect('home:home')
        return render(request, self.template_name, {'form':form})

my urls 

from django.urls import path
from . import views
app_name = 'accounts'

urlpatterns = [
    path('register/', views.UserRegisterView.as_view(), name='user_register'),
]
my templates 

{% load static %}
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link
        href="https://fonts.googleapis.com/css2?family=Fira+Sans:ital,wght@1,500&family=Josefin+Sans&family=Poppins:ital,wght@0,400;0,700;1,300&family=Roboto&family=Rubik+Pixels&family=Yanone+Kaffeesatz&display=swap"
        rel="stylesheet">
    <link rel="stylesheet" href="{% static 'css\signup.css' %}">
    <meta name="viewport" content="width=\, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
    <title>signup now</title>
</head>
{% block content %}
    <div class="login">
        <h3 class="text-center">Signup Now</h3>
        <hr>
        <form method="post" action="{% url 'accounts:user_register' %}" novalidate>
            {% csrf_token %}
            
            <div class="form-group sm-3 ">
                <label class="form-label" for="email">Email Address:</label>
                <input class="form-control rounded-pill" type="email" id="email" placeholder="Enter your Email "
                    required>
                <div class="invalid-feedback">
                    please enter your email address
                </div>
            </div>

            <div class="form-group sm-3 ">
                <label class="form-label" for="username">Username:</label>
                <input class="form-control rounded-pill" type="uername" id="username" placeholder="Choase a Username "
                    required>
                <div class="invalid-feedback">
                    please enter your valid username
                </div>
            </div>

            <div class="form-group">
                <label class="form-label" for="password">Password:</label>
                <input class="form-control rounded-pill" type="password" id="password" placeholder="Enter your Password"
                    required>
                <div class="invalid-feedback">
                    please enter your password
                </div>
            </div>

            <div class="form-group">
                <label class="form-label" for="password">Confirm Password:</label>
                <input class="form-control rounded-pill" type="password" id="password" placeholder="Confirm your Password"
                    required>
                <div class="invalid-feedback">
                    please confirm your password
                </div>
            </div>

            <input class="btn btn-success w-100 rounded-pill " type="submit" value="Register">

        </form>
    </div>
    {% endblock %}
my forms 
from django import forms
from django.contrib.auth.models import User
from django.core.exceptions import ValidationError

class UserRegistrationForm(forms.Form):
    username = forms.CharField(widget=forms.TextInput(attrs={'class':'form-control'}))
    email = forms.EmailField(widget=forms.EmailInput(attrs={'class':'form-control'}))
    password1 = forms.CharField(label='password', widget=forms.PasswordInput(attrs={'class':'form-control'}))
    password2 = forms.CharField(label='confirm password', widget=forms.PasswordInput(attrs={'class':'form-control'}))
 
    def clean_email(self):
        email = self.cleaned_data['email']
        user = User.objects.filter(email=email).exists()
        if user:
            raise ValidationError('this email already exists')
        return email

    def clean(self):
        cd = super().clean()
        p1 = cd.get('password1')
        p2 = cd.get('password2')

        if p1 and p2 and p1 != p2:
            raise ValidationError('password must match')

Hello, I have created a user registration system, but when I fill in the information on the registration page and click on registration, no user is created and only the same form is displayed on the page, please guide me.

Upvotes: 0

Views: 32

Answers (1)

Mahammadhusain kadiwala
Mahammadhusain kadiwala

Reputation: 3650

Here mistake in html form, I can see you are already created Django form but not used in html normal html field not have form.cleaned_data, only have Django form

if you want still use normal html form input you need to get input data using request.POST.get('username') instead of using cd = form.cleaned_data

but, if you want use Django form then you can use form.cleaned_data your html code become like this if you used Django form

HTML code using Django form

{% load static %}
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link
        href="https://fonts.googleapis.com/css2?family=Fira+Sans:ital,wght@1,500&family=Josefin+Sans&family=Poppins:ital,wght@0,400;0,700;1,300&family=Roboto&family=Rubik+Pixels&family=Yanone+Kaffeesatz&display=swap"
        rel="stylesheet">
    <link rel="stylesheet" href="{% static 'css\signup.css' %}">
    <meta name="viewport" content="width=\, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
    <title>signup now</title>
</head>
{% block content %}
<div class="login">
    <h3 class="text-center">Signup Now</h3>
    <hr>
    <form method="post" action="{% url 'accounts:user_register' %}" novalidate>
        {% csrf_token %}

        <div class="form-group sm-3 ">
            {{form.as_p}}
        </div>
        <input class="btn btn-success w-100 rounded-pill " type="submit" value="Register">
    </form>
</div>
{% endblock %}

Upvotes: 0

Related Questions