Reputation: 179
I'm creating a form but the fields to input passwords are not appearing. I can only see the values to input first name, last name and email address. Please any help I would really appreciate it. The code works as following:
First there is a model in where there are the 5 values that I want to be printed. Then the form values are printed individually in the HTML code.
HTML code:
<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>
forms.py:
class CreateUserForm(forms.ModelForm):
class Meta:
model = User
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 = '__all__'
models.py:
class User(models.Model):
first_name = forms.CharField()
last_name = forms.CharField()
email = forms.EmailField()
password1 = forms.CharField()
password2 = forms.CharField()
Upvotes: 0
Views: 323
Reputation: 51938
The question is are you using the right User
model when you defining it in your Form Meta's model attribute:
from .models import User
class CreateUserForm(forms.ModelForm):
class Meta:
model = User
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 = '__all__'
To be honest, it is best to use Django's built-in User
model or customized version of the User model. If you use Django's built-in model, then you can simply use UserCreationForm
, which provides user creation, password validation, password hashing etc.
from django.contrib.auth.forms import UserCreationForm
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']
Upvotes: 2
Reputation: 2167
probably error there, it should be
class User(models.Model):
first_name = models.CharField()
last_name = models.CharField()
email = models.EmailField()
password1 = models.CharField()
password2 = models.CharField()
also check this answer How to create Password Field in Model Django
Upvotes: 0