0

I'm creating a registration page in Django and everything works fine, and gets rendered correctly except the password fields. Why can that due to? I've tried building a form without the inbuild Django methods but it doesn't work either. It is very weird as I think all the code is working fine and everything is build correctly.

Here is the html:

<body class="bg-gradient-primary">

    <div class="container">

        <div class="card o-hidden border-0 shadow-lg my-5">
            <div class="card-body p-0">
                <!-- Nested Row within Card Body -->
                <div class="row">
                    <div class="col-lg-5 d-none d-lg-block bg-register-image"></div>
                    <div class="col-lg-7">
                        <div class="p-5">
                            <div class="text-center">
                                <h1 class="h4 text-gray-900 mb-4">Create an Account!</h1>
                            </div>
                            <form class="user" method="POST">
                              {% csrf_token %}
                                <div class="form-group row">
                                    <div class="col-sm-6 mb-3 mb-sm-0">
                                      {{ form.first_name }}

                                    </div>
                                    <div class="col-sm-6">
                                        {{ form.last_name }}


                                    </div>
                                </div>
                                <div class="form-group">
                                  {{ form.email }}

                                </div>
                                <div class="form-group row">
                                    <div class="col-sm-6 mb-3 mb-sm-0">
                                      {{ form.password1 }}

                                    </div>
                                    <div class="col-sm-6">
                                        {{ form.password2 }}

                                    </div>
                                </div>

                                <button type="submit" class="btn btn-primary btn-user btn-block">
                                    Register Account
                                </button>

                                <hr>
                                <a href="index.html" class="btn btn-google btn-user btn-block">
                                    <i class="fab fa-google fa-fw"></i> Register with Google
                                </a>
                                <a href="index.html" class="btn btn-facebook btn-user btn-block">
                                    <i class="fab fa-facebook-f fa-fw"></i> Register with Facebook
                                </a>
                            </form>
                            <hr>
                            <div class="text-center">
                                <a class="small" href="forgot-password.html">Forgot Password?</a>
                            </div>
                            <div class="text-center">
                                <a class="small" href="login.html">Already have an account? Login!</a>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

    </div>

    <!-- Bootstrap core JavaScript-->
    <script src='{% static "vendor/jquery/jquery.min.js" %}'></script>
  <script src='{% static "vendor/bootstrap/js/bootstrap.bundle.min.js" %}'></script>

    <!-- Core plugin JavaScript-->
    <script src='{% static "vendor/jquery-easing/jquery.easing.min.js" %}'></script>

    <!-- Custom scripts for all pages-->
      <script src='{% static "js/sb-admin-2.min.js" %}'></script>

</body>

</html>

Here is the forms.py:

from django.forms import ModelForm
from django.contrib.auth.forms import UserCreationForm
from django import forms
from django.contrib.auth.models import User
from django.forms import ModelForm, TextInput, PasswordInput, EmailInput
from django.contrib.auth import get_user_model

class CreateUserForm(UserCreationForm):
    class Meta(UserCreationForm.Meta):
        model = get_user_model()
        widgets = {
            'first_name': TextInput(attrs={'class': 'form-control form-control-user', 'placeholder': 'First Name'}),
            'last_name': TextInput(attrs={'class': 'form-control form-control-user', 'placeholder': 'Last Name'}),
            'email': EmailInput(attrs={'class': 'form-control form-control-user', 'placeholder': 'Email Address'}),
            'password1': PasswordInput(attrs={'class': 'form-control form-control-user', 'placeholder': 'Password'}),
            'password2': PasswordInput(attrs={'class': 'form-control form-control-user', 'placeholder': 'Repeat Password'}),
        }
        fields = ['first_name', 'last_name', 'email']

Here is the models.py

from django.db import models
from django.contrib.auth.forms import UserCreationForm
from django import forms
# Create your models here.
class User(models.Model):
    first_name = models.CharField(max_length=40000)
    last_name = models.CharField(max_length=40000)
    email = models.EmailField(max_length=40000)
    password1 = models.CharField(max_length=40000)
    password2 = models.CharField(max_length=40000)

class Emails(models.Model):
    name = models.CharField(max_length=40000)
    urls = models.URLField(max_length=200)
    emails = models.TextField()
    
    def __str__(self):
        return self.name

Here is the register page:

def register(request):

    if request.user.is_authenticated:
        return redirect('login')
    else:

        form = CreateUserForm()
        if request.method == 'POST':
            form = CreateUserForm(request.POST)
            if form.is_valid():
                form.save()
                messages.success(request, 'Account was created for' + user)
                return redirect('login')

    context = {'form':form}

    return render(request, 'leadfinderapp/register.html', context)

Here is an image of the password fields

1

1 Answer 1

0

EDIT


The parts you added allowed me to reproduce the problem

This works for me

class CreateUserForm(UserCreationForm):
    class Meta(UserCreationForm.Meta):
        model = get_user_model()
        widgets = {
            'first_name': TextInput(attrs={'class': 'form-control form-control-user', 'placeholder': 'First Name'}),
            'last_name': TextInput(attrs={'class': 'form-control form-control-user', 'placeholder': 'Last Name'}),
            'email': EmailInput(attrs={'class': 'form-control form-control-user', 'placeholder': 'Email Address'}),
        }
        fields = ['first_name', 'last_name', 'email']

    def __init__(self, *args, **kwargs):
        super(CreateUserForm, self).__init__(*args, **kwargs)
        self.fields['password1'].widget = PasswordInput(
            attrs={'class': 'form-control', 'placeholder': 'Password'})
        self.fields['password2'].widget = PasswordInput(
            attrs={'class': 'form-control', 'placeholder': 'Repeat Password'})

The answer is taken from here:

Sign up to request clarification or add additional context in comments.

4 Comments

already tried, but when I put that, the server gets an error saying that those fields dont exist. What code you want of the project?
Another attempt I would make is to put fields on top of widgets. If that doesn't work, add models.py and views.py files
still not working after putting fields above widgets. I've added models and views.
This didn't work for me but thank you anyways! I've posted the answer in comments of my question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.