user1050632 user1050632 - 1 year ago 175
Python Question

django request.POST data is empty but works with other similar form

I'm new to django and I was following a tutorial from django on how to process POST form data with a model

I was able to do that for a simple login, and I can print out the variables in the console. The form.is_valid() function is true and works as expected for the login view.

I did the same exact thing for a registration page and I'm getting FALSE returned from the is_valid() function. I was tinkering with csrf tokens and that didn't seem to be causing the issue and thats why I didn't require them.

I think it's going to be a silly problem because I'm able to get the request.POST in the login case but not the registration. Any help is appreciated.

Here is my html form

<div id="login" class="animate form">
<form action="/signin" autocomplete="on" method ="POST">
<!--{% csrf_token} -->
<h1>Log in</h1>
<label for="username" class="uname" data-icon="u" > Your email or username </label>
<input id="username" name="username" required="required" type="text" placeholder="myusername or"/>
<label for="password" class="youpasswd" data-icon="p"> Your password </label>
<input id="password" name="password" required="required" type="password" placeholder="eg. X8df!90EO" />
<p class="keeplogin">
<input type="checkbox" name="loginkeeping" id="loginkeeping" value="loginkeeping" />
<label for="loginkeeping">Keep me logged in</label>
<p class="login button">
<input type="submit" value="Login" />

<div id="register" class="animate form">
<form action="/register" autocomplete="on" method ="POST">
<!--{% csrf_token %} -->
<h1> Sign up </h1>
<label for="usernamesignup" class="uname" data-icon="u">Your username</label>
<input id="usernamesignup" name="usernamesignup" required="required" type="text" placeholder="mysuperusername690" />
<label for="emailsignup" class="youmail" data-icon="e" > Your email</label>
<input id="emailsignup" name="emailsignup" required="required" type="email" placeholder=""/>
<label for="passwordsignup" class="youpasswd" data-icon="p">Your password </label>
<input id="passwordsignup" name="passwordsignup" required="required" type="password" placeholder="eg. X8df!90EO"/>
<label for="passwordsignup_confirm" class="youpasswd" data-icon="p">Please confirm your password </label>
<input id="passwordsignup_confirm" name="passwordsignup_confirm" required="required" type="password" placeholder="eg. X8df!90EO"/>
<p class="signin button">
<input type="submit" value="Sign up"/>

Here is the

from django import forms
from django.forms import CharField

class NameForm(forms.Form):
username = forms.CharField(label = 'username', max_length=25)
password = forms.CharField(label = 'password', max_length=25)

class RegForm(forms.Form):
regName = forms.CharField(label = 'usernamesignup', max_length = 25)
regEmail = forms.CharField(label = 'emailsignup', max_length = 50)
regPassword = forms.CharField(label = 'passwordsignup', max_length = 30)
regPasswordConfirm = forms.CharField(label = 'passwordsignup_confirm', max_length = 30)

Here is the that's handling login/registration (this is rough draft)

def signin(request):
#if this is a POST request we need to process the login credentials
if request.method == 'POST':
#create the form instance and populate with username/password
form = NameForm(request.POST)
print form
if form.is_valid():
username = form.cleaned_data['username']
password = form.cleaned_data['password']
print username
print password
return render(request, 'webServer/home.html')
return render(request, 'webServer/login.html')
return render(request, 'webServer/login.html')

def register(request):
if request.method == 'POST':
#create form instance and grab register credentials
form2 = RegForm(request.POST)
#verify not a duplicate entry (email, username)
print form2
if form2.is_valid():
username = form2.cleaned_data['regPassword']
return render(request, 'webServer/home.html')
print 'had error'
return render_to_response('webServer/errors.html', {'form': form2})
return render(request, 'webServer/login.html')

Answer Source

You seem to be using completely different field names in the template from the form - your form has regName, regEmail etc, but your template has usernamesignup etc.

In any case, you should be using the form object itself to output the fields:

{{ form.regName.label_tag }}
{{ form.regName }}
{{ form.regName.errors }}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download