Reputation: 573
I have this html form:
<form method="post" action="">
{% csrf_token %}
<div class="inputText">
<input type="text" name="username" placeholder="Username" value = required />
<br><br>
<input type="email" name="email" placeholder="Email" required />
<br><br>
<input type="password" name="pass1" id="pass1" placeholder="Password" required />
<br><br>
<input type="password" name="pass2" id="pass2" onkeyup="checkPass(); return false;" placeholder="Confirm Password" required />
<br><br>
<span id="confirmMessage" class="confirmMessage"></span>
</div>
<div class="send">
<input type="submit" name="register" value="Register" class="register" />
</div>
This is my forms.py :
class UserForm(forms.ModelForm):
class Meta:
model = User
fields = ('username', 'email', 'password')
And my view
def register(request):
if request.method == "POST":
form = UserForm(request.POST)
if form.is_valid():
form.save
new_user = authenticate(username=request.POST['username'], password =request.POST['password'])
login(request.new_user)
return HttpResponseRedirect(request, '/index')
else:
form = UserForm()
return render(request, 'authen/register.html', {'form': form})
This should be a register system but when I press register it reloads the register page and nothing. When I go to django admin I see no new user so django is not taking input from form fields.
Upvotes: 0
Views: 4177
Reputation: 469
Be sure to get
the input element by its name
attribute. This is because request.POST['keyword']
refers to the element identified by the specified html name
attribute keyword
.
Here's an example:
<form action="/login/" method="POST">
<input type="text" name="keyword" placeholder="Search query">
<input type="number" name="results" placeholder="Number of results">
</form>
In your Python file, where you get the value of the input elements, request.POST['keyword']
and request.POST['results']
will contain the values of keyword
and results
, respectively.
Upvotes: 0
Reputation: 599610
The form is presumably not valid, but you are not displaying any errors in your template. At least do {{ form.errors }}
somewhere, although really you should output all the fields and their errors from the Django form directly:
{{ form.username }}
{{ form.username.errors }}
etc.
Also note you are not actually invoking the save method in your is_valid block. You need to do:
form.save()
Upvotes: 3