[英]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我是 django 的新手,我正在学习 django 关于如何使用模型处理 POST 表单数据的教程
https://docs.djangoproject.com/en/dev/topics/forms/#using-a-form-in-a-view 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.
form.is_valid() 函数为 true 并且按预期用于登录视图。
I did the same exact thing for a registration page and I'm getting FALSE returned from the is_valid() function.我对注册页面做了同样的事情,我从 is_valid() 函数返回了 FALSE。 I was tinkering with csrf tokens and that didn't seem to be causing the issue and thats why I didn't require them.
我正在修补 csrf 令牌,这似乎并没有导致问题,这就是我不需要它们的原因。
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.我认为这将是一个愚蠢的问题,因为我能够在登录情况下获得 request.POST 而不是注册。 Any help is appreciated.
任何帮助表示赞赏。
Here is my html form这是我的 html 表单
<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这是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)这是处理登录/注册的 views.py(这是草稿)
@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')
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.您似乎在模板中使用了与表单完全不同的字段名称 - 您的表单具有
regName
、 regEmail
等,但您的模板具有usernamesignup
等。
In any case, you should be using the form object itself to output the fields:在任何情况下,您都应该使用表单对象本身来输出字段:
{{ form.regName.label_tag }}
{{ form.regName }}
{{ form.regName.errors }}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.