Django Class based views - Signup

Viewed 130

I have implemented a SignUp/SignIn based on class based views. While the Login is working fine, Sign up it does not. The problem is that after trying to register user and POST method is 200, user is not created in database. BTW I think the problem is on the FrontEnd because when i'm using a simple html file with {{ form.as_p }} it works when i'm using a template from internet it does not. Please help me to identify the reason:

views.py file

from django.shortcuts import render
from django.views import generic
from django.contrib.auth.forms import UserCreationForm
from django.urls import reverse_lazy


class UserResisterView(generic.CreateView):
    form_class = UserCreationForm
    template_name = 'registration/register.html'
    success_url = reverse_lazy('login')
    success_message = "Your profile was created successfully"

authapp1/urls.py file

from django.urls import path
from .views import UserResisterView

urlpatterns = [
    path('register/', UserResisterView.as_view(), name='register'),
]

auth/urls.py file

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('pages.urls')),
    # Django authentication system with package URLs
    path('authapp1/', include('django.contrib.auth.urls')),
    path('authapp1/', include('authapp1.urls')),

html template

<!-- Register -->
<section class="our-log-reg bgc-fa">
    <div class="container">
        <div class="row">
            <div class="col-sm-12 col-lg-6 offset-lg-3">
                <div class="sign_up_form inner_page">
                    <div class="heading">
                        <h3 class="text-center">Register to start learning</h3>
                        <p class="text-center">Have an account? <a class="text-thm" href="{% url 'login' %}">Login</a></p>
                    </div>
                    <div class="details">
                        <form action="{% url 'register' %}" form method="POST">
                            {% csrf_token %}
                            <div class="form-group">
                                <input type="username" class="form-control" id="exampleInputName2" placeholder="Username" name="username">
                            </div>
                            <div class="form-group">
                                <input type="password" class="form-control" id="exampleInputPassword4" placeholder="Password" name="password1">
                            </div>
                            <div class="form-group">
                                <input type="password" class="form-control" id="exampleInputPassword5" placeholder="Confirm Password" name="password2">
                            </div>
                            <div class="form-group custom-control custom-checkbox">
                                <input type="checkbox" class="custom-control-input" id="exampleCheck3">
                                <label class="custom-control-label" for="exampleCheck3">Want to become an instructor?</label>
                            </div>
                            {{ form.non_field_errors }}
                            <button type="submit" class="btn btn-log btn-block btn-thm2">Register</button>
                            <div class="divide">
                                <span class="lf_divider">Or</span>
                                <hr>
                            </div>
                            <div class="row mt40">
                                <div class="col-lg">
                                    <button type="submit" class="btn btn-block color-white bgc-fb mb0"><i class="fa fa-facebook float-left mt5"></i> Facebook</button>
                                </div>
                                <div class="col-lg">
                                    <button type="submit" class="btn btn2 btn-block color-white bgc-gogle mb0"><i class="fa fa-google float-left mt5"></i> Google</button>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>

Login template that works fine:

<!-- LogIn -->
    <section class="our-log bgc-fa">
        <div class="container">
            <div class="row">
                <div class="col-sm-12 col-lg-6 offset-lg-3">
                    <div class="login_form inner_page">
                        <form action="{% url 'login' %}" form method="POST">
                            {% csrf_token %}
                            <div class="heading">
                                <h3 class="text-center">Login to your account</h3>
                                <p class="text-center">Don't have an account? <a class="text-thm" href="{% url 'register' %}">Sign Up!</a></p>
                            </div>
                            {{ form.non_field_errors }}
                             <div class="form-group">
                                <input type="username" class="form-control" id="exampleInputEmail3" placeholder="Username" name="username">
                            </div>
                            <div class="form-group">
                                <input type="password" class="form-control" id="exampleInputPassword4" placeholder="Password" name="password">
                            </div>
                            <div class="form-group custom-control custom-checkbox">
                                <input type="checkbox" class="custom-control-input" id="exampleCheck3">
                                <label class="custom-control-label" for="exampleCheck3">Remember me</label>
                                <a class="tdu btn-fpswd float-right" href="#">Forgot Password?</a>
                            </div>
                            <button type="submit" class="btn btn-log btn-block btn-thm2">Login</button>
                            <div class="divide">
                                <span class="lf_divider">Or</span>
                                <hr>
                            </div>
                            <div class="row mt40">
                                <div class="col-lg">
                                    <button type="submit" class="btn btn-block color-white bgc-fb mb0"><i class="fa fa-facebook float-left mt5"></i> Facebook</button>
                                </div>
                                <div class="col-lg">
                                    <button type="submit" class="btn btn2 btn-block color-white bgc-gogle mb0"><i class="fa fa-google float-left mt5"></i> Google</button>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </section>
1 Answers

Try rendering the form using:

{{ form.as_p }}

If it doesn't work still, paste the request body that is sent with the form, so we can check it out. If it works it means that your inputs have wrong names/ids.

Edit: the field names are incorrect:

                             <div class="form-group">
                                <input type="username" class="form-control" id="exampleInputEmail3" placeholder="Username" name="username">
                            </div>
                            <div class="form-group">
                                <input type="password" class="form-control" id="exampleInputPassword4" placeholder="Password" name="password">
                            </div>

to become:

                             <div class="form-group">
                                <input type="username" class="form-control" id="exampleInputEmail3" placeholder="Username" name="username">
                            </div>
                            <div class="form-group">
                                <input type="password" class="form-control" id="exampleInputPassword4" placeholder="Password" name="password1">
                            </div>
                            <div class="form-group">
                                <input type="password" class="form-control" id="exampleInputPassword4" placeholder="Password" name="password2">
                            </div>

Edit 2: Added references UserCreationForm has "password1" and "password2" as mandatory fields. https://docs.djangoproject.com/en/1.8/_modules/django/contrib/auth/forms/

Related