Reputation: 861
I'm facing some difficulties while trying to create a new user using UserCreationForm in Django. I think that the issue is on the signup HTML page, but I can't really understand what am I doing wrong. When I click submit after filling the required fields nothing happens. I hope you could help me out.
Part of signup.html:
<div class="container">
<form id="register-form" method="post">
{% csrf_token %}
{% for field in form %}
{% if field.name == 'first_name' %}
<div class="form-group">
<input id="register-first_name" type="text" name="registerFirst" required
class="input-material">
<label for="register-first_name" class="label-material">{{ field.label_tag }}</label>
</div>
{% endif %}
{% if field.name == 'last_name' %}
<div class="form-group">
<input id="register-last_name" type="text" name="registerLast" required
class="input-material">
<label for="register-last_name" class="label-material">{{ field.label_tag }}</label>
</div>
{% endif %}
{% if field.name == 'username' %}
<div class="form-group">
<input id="username" type="text" name="username" required
class="input-material">
<label for="username" class="label-material">{{ field.label_tag }}</label>
</div>
{% endif %}
{% if field.name == 'email' %}
<div class="form-group">
<input id="register-email" type="text" name="registerEmail" required
class="input-material">
<label for="register-email" class="label-material">{{ field.label_tag }}</label>
</div>
{% endif %}
{% if field.name == 'password' %}
<div class="form-group">
<input id={{ form.name.name }} type="text" name="password" required
class="input-material">
<label for={{ form.name.name }} class="label-material">{{ field.label_tag }}</label>
</div>
{% endif %}
{% endfor %}
<input id="register" type="submit" value="Register" class="btn btn-primary">
</form>
<div/>
my forms.py:
from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
class SignUpForm(UserCreationForm):
first_name = forms.CharField(max_length=30, required=False, help_text='Optional.')
last_name = forms.CharField(max_length=30, required=False, help_text='Optional.')
email = forms.EmailField(max_length=254, help_text='Required. Inform a valid email address.')
class Meta:
model = User
fields = ('username', 'first_name', 'last_name', 'email', 'password', )
def save(self, commit=True):
user = super(SignUpForm, self).save(commit=False)
user.email = self.cleaned_data['email']
user.first_name = self.cleaned_data['first_name']
user.last_name = self.cleaned_data['last_name']
user.save()
my views.py:
from django.http import HttpResponse
from django.shortcuts import render
from django.contrib.auth import authenticate
from django.contrib.auth import login as login_a
from django.shortcuts import render, redirect
from movie.forms import SignUpForm
def index(request):
return render(request, 'home.html')
def login(request):
return render(request,'login.html')
def logout(request):
return render(request,'logout.html')
def signup(request):
if request.method == 'POST':
form = SignUpForm(request.POST)
if form.is_valid():
form.save()
username = form.cleaned_data.get('username')
raw_password = form.cleaned_data.get('password')
print (username);
print (raw_password);
user = authenticate(username=username, password=raw_password)
login_a(request, user)
return redirect('login')
else:
form = SignUpForm()
return render(request, 'signup.html', {'form': form})
Upvotes: 0
Views: 62
Reputation: 308779
Your form looks incorrect. You are using incorrect values for the names (e.g. name="registerFirst"
instead of name="first_name"
), so the Django form will not use the submitted values. Then you are not displaying the field errors, so you don't get any useful feedback when you submit the form.
I would start by letting Django render the form. That will let you fix any problems in the view.
<form id="register-form" method="post">
{% csrf_token %}
{{ form.as_p }}
<input id="register" type="submit" value="Register" class="btn btn-primary">
</form>
To help debugging, you can print values in your view, for example add print(form.errors)
after the if form.is_valid():
block.
Then, once the view is working with the simple template, you can render the fields manually if you need more control over the layout. See the docs on rendering fields manually for more info. If you do this, remember to display any field/form errors to the user.
If you do render the fields manually, you can access the fields with {{ form.first_name }}
, instead of looping through {% for field in form %}
then having to check field.name
.
Upvotes: 3