user1050632 user1050632 - 2 months ago 4
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

https://docs.djangoproject.com/en/dev/topics/forms/#using-a-form-in-a-view

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>
<p>
<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 mymail@mail.com"/>
</p>
<p>
<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>
<p class="keeplogin">
<input type="checkbox" name="loginkeeping" id="loginkeeping" value="loginkeeping" />
<label for="loginkeeping">Keep me logged in</label>
</p>
<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>
<p>
<label for="usernamesignup" class="uname" data-icon="u">Your username</label>
<input id="usernamesignup" name="usernamesignup" required="required" type="text" placeholder="mysuperusername690" />
</p>
<p>
<label for="emailsignup" class="youmail" data-icon="e" > Your email</label>
<input id="emailsignup" name="emailsignup" required="required" type="email" placeholder="mysupermail@mail.com"/>
</p>
<p>
<label for="passwordsignup" class="youpasswd" data-icon="p">Your password </label>
<input id="passwordsignup" name="passwordsignup" required="required" type="password" placeholder="eg. X8df!90EO"/>
</p>
<p>
<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>
<p class="signin button">
<input type="submit" value="Sign up"/>


Here is the forms.py

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 views.py that's handling login/registration (this is rough draft)

@csrf_exempt
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)
#verify
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')
else:
return render(request, 'webServer/login.html')
else:
return render(request, 'webServer/login.html')

@csrf_exempt
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')
else:
print 'had error'
return render_to_response('webServer/errors.html', {'form': form2})
else:
return render(request, 'webServer/login.html')

Answer

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 }}
Comments