I have a strange problem with a form I have created for user registration using a Django customer user model. I'm running Django 1.6.5.
我有一个奇怪的问题,我用Django客户用户模型为用户注册创建了一个表单。我运行Django 1.6.5。
forms.py
forms.py
class SignupForm(forms.ModelForm):
password1 = forms.CharField(widget=forms.PasswordInput,
label=_('Password'),
max_length=50)
password2 = forms.CharField(widget=forms.PasswordInput,
label=_('Repeat password'),
max_length=50)
class Meta:
model = RegisteredUser
fields = ("firstname", "lastname", "email", "password1", "password2",)
def clean_email(self):
existing = RegisteredUser.objects.filter(email__iexact=self.cleaned_data['email'])
if existing.exists():
raise forms.ValidationError(_('A user with that email already exists.'))
return self.cleaned_data['email']
def clean(self):
cleaned_data = super(SignupForm, self).clean()
if 'password1' in self.cleaned_data and 'password2' in self.cleaned_data:
if self.cleaned_data['password1'] != self.cleaned_data['password2']:
raise forms.ValidationError(_('The two passwords did not match.'))
return cleaned_data
views.py
views.py
class SignUpView(FormView):
template_name = 'core/signup.html'
form_class = SignupForm
success_url = None
def post(self, request, *args, **kwargs):
form = self.get_form(self.get_form_class())
if form.is_valid():
return self.form_valid(request, form)
else:
return self.form_invalid(form)
def form_valid(self, request, form):
new_user = self.register(request, **form.cleaned_data)
success_url = self.get_success_url(request, new_user)
try:
to, args, kwargs = success_url
return redirect(to, *args, **kwargs)
except ValueError:
return redirect(success_url)
def register(self, request, **cleaned_data):
RegisteredUser.objects.create_user(email=cleaned_data['username'],
firstname=cleaned_data['firstname'],
lastname=cleaned_data['lastname'],
password=cleaned_data['password2'])
The form returns invalid saying that 'password1' and 'password2' are required fields. The value is clearly present in the post data along with the other fields.
表单返回无效,表示“password1”和“password2”是必需的字段。该值与其他字段一起清晰地显示在post数据中。
No matter what I have tried, 'password1' and 'password2' are not included in the cleaned_data dict. I have tried using various other names for these two fields, just to make sure 'password2' wasn't conflicting anything else.
无论我尝试了什么,“password1”和“password2”都不包含在cleaned_data dict中。
I'm probably overlooking something extremely simple :-)
我可能忽略了一些非常简单的东西:-)
Thanks!
谢谢!
Mike
迈克
EDIT 1 - the template in use (sorry for the div-hell!):
编辑1 -正在使用的模板(对不起,该死的!)
<section class="container">
<div class="row">
<div class="col-md-6 col-sm-6">
<h2>Create an <strong>Account</strong></h2>
<form class="white-row" method="post" action="#">
{% csrf_token %}
<div class="row">
<div class="form-group">
<div class="col-md-12 col-sm-12">
<label>Firstname</label>
<input type="text" class="form-control" name="firstname">
</div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-md-12 col-sm-12">
<label>Lastname</label>
<input type="text" class="form-control" name="lastname">
</div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-md-12 col-sm-12">
<label>Email</label>
<input type="email" class="form-control" name="email">
</div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-md-6 col-sm-6">
<label>Password</label>
<input type="password" class="form-control" name="passsword1">
</div>
<div class="col-md-6 col-sm-6">
<label>Repeat password</label>
<input type="password" class="form-control" name="passsword2">
</div>
</div>
</div>
<div class="row">
<div class="col-md-12 col-sm-12">
<input type="submit" value="Sign up" class="btn btn-primary pull-right push-bottom" data-loading-text="Loading...">
</div>
</div>
</form>
</div>
1 个解决方案
#1
1
Here it is an example of how I do this in my projects:
这是我在项目中如何做到这一点的一个例子:
forms.py:
forms.py:
class NewUserForm(forms.Form):
username = ...
password1 = forms.CharField(widget=forms.PasswordInput, required=True)
password2 = forms.CharField(widget=forms.PasswordInput, required=True)
def clean_password2(self):
password1 = self.cleaned_data.get("password1", "")
password2 = self.cleaned_data.get("password2", "")
if password1 and password2: # If both passwords has value
if password1 != password2:
raise forms.ValidationError(_(u"Passwords didn't match."))
else:
raise forms.ValidationError(_(u"Passwords can't be blank."))
return password2
views.py:
views.py:
@login_required
def add_user(request):
''' Adds new user '''
if request.method == 'POST':
form = NewUserForm(request.POST)
if form.is_valid():
user = form.save()
# TODO: Save in temporary table
return HttpResponse('SUBMIT')
else:
form = NewUserForm()
return render_to_response('add_user.html', {'form': form}, context_instance=RequestContext(request))
template (form example):
模板(形式):
<form action="." method="post" id="add_user_form">
{% csrf_token %}
{{ form.username }}
{{ form.password1 }}
{{ form.password2 }}
{{ form.usertype }}
<input type="submit" value="{% trans 'Save' %}" class="default"/>
</form>
{% csrf_token %}
: You need to put this in every form you use
{% csrf_token %}:您需要在使用的每个窗体中都这样做
action="."
: This make the post to the actual page
action =“。”:这将使文章进入实际页面
{{ form.VARIABLE_NAME}}
You need to set the inputs of the form like this, you create the form in the view, send to the template, and use it as a Django variable using {{ }}
. To set an input for one of your fields, like password1
it will be like I wrote above {{ form.password1}}
, you can also use {{form.errors}}
to check all form errors, or {{form.VARIABLE_NAME.errors}}
to check for exact field errors in the template
{ {形式。需要像这样设置表单的输入、在视图中创建表单、发送到模板并使用{{}将其作为Django变量。要为您的一个字段设置一个输入,比如password1,就像我在{{{form}上面所写的那样。password1},您还可以使用{{form。{errors}用于检查所有的表单错误,或{{{form. variable_name。}用于检查模板中准确的字段错误
#1
1
Here it is an example of how I do this in my projects:
这是我在项目中如何做到这一点的一个例子:
forms.py:
forms.py:
class NewUserForm(forms.Form):
username = ...
password1 = forms.CharField(widget=forms.PasswordInput, required=True)
password2 = forms.CharField(widget=forms.PasswordInput, required=True)
def clean_password2(self):
password1 = self.cleaned_data.get("password1", "")
password2 = self.cleaned_data.get("password2", "")
if password1 and password2: # If both passwords has value
if password1 != password2:
raise forms.ValidationError(_(u"Passwords didn't match."))
else:
raise forms.ValidationError(_(u"Passwords can't be blank."))
return password2
views.py:
views.py:
@login_required
def add_user(request):
''' Adds new user '''
if request.method == 'POST':
form = NewUserForm(request.POST)
if form.is_valid():
user = form.save()
# TODO: Save in temporary table
return HttpResponse('SUBMIT')
else:
form = NewUserForm()
return render_to_response('add_user.html', {'form': form}, context_instance=RequestContext(request))
template (form example):
模板(形式):
<form action="." method="post" id="add_user_form">
{% csrf_token %}
{{ form.username }}
{{ form.password1 }}
{{ form.password2 }}
{{ form.usertype }}
<input type="submit" value="{% trans 'Save' %}" class="default"/>
</form>
{% csrf_token %}
: You need to put this in every form you use
{% csrf_token %}:您需要在使用的每个窗体中都这样做
action="."
: This make the post to the actual page
action =“。”:这将使文章进入实际页面
{{ form.VARIABLE_NAME}}
You need to set the inputs of the form like this, you create the form in the view, send to the template, and use it as a Django variable using {{ }}
. To set an input for one of your fields, like password1
it will be like I wrote above {{ form.password1}}
, you can also use {{form.errors}}
to check all form errors, or {{form.VARIABLE_NAME.errors}}
to check for exact field errors in the template
{ {形式。需要像这样设置表单的输入、在视图中创建表单、发送到模板并使用{{}将其作为Django变量。要为您的一个字段设置一个输入,比如password1,就像我在{{{form}上面所写的那样。password1},您还可以使用{{form。{errors}用于检查所有的表单错误,或{{{form. variable_name。}用于检查模板中准确的字段错误