0

当我填写表格并提交时,不会将任何内容添加到数据库中,而是使用空密码字段重新加载页面,即使表格包含的密码不等于验证密码 ValidationError 也不会出现......谢谢你

表格.py

class SignUpForm(ModelForm):
    username      = forms.CharField(label=(u'Username'))
    first_name    = forms.CharField(label=(u'First name'))
    last_name     = forms.CharField(label=(u'Last name'))
    address       = forms.CharField(label=(u'Address'))
    email         = forms.EmailField(label=(u'Email Address'))
    password      = forms.CharField(label=(u'Password'),widget=forms.PasswordInput(render_value=False))
    password1     = forms.CharField(label=(u'Verify Password'),widget=forms.PasswordInput(render_value=False))
    mobile        = forms.IntegerField(label=(u'Mobile'))

    class Meta:
        model = Employer
        ## check whats Exclude !!!!
        exclude = ('user',)

def clean_username(self):
        username = self.cleaned_data['username']
        try:
            User.objects.get(username=username)
        except User.DoesNotExist:
            return username

            raise forms.ValidationError("That username  already exists. Please select another")

def clean(self):
        if self.cleaned_data['password'] != self.cleaned_data['password1']:
            raise forms.ValidationError("not matched")
        return self.cleaned_data

视图.py

def EmployerRegistration(request):
    if request.user.is_authenticated():
        return HttpResponseRedirect('/profile/')
    if request.method == 'POST':
        form = SignUpForm(request.POST)
        if form.is_valid():
            employer =Employer.objects.create(username=form.cleaned_data["username"],password= form.cleaned_data['password'],first_name=form.cleaned_data['first_name'],last_name=form.cleaned_data['last_name'],email=form.cleaned_data['email'],address=form.cleaned_data['address'],mobile=form.cleaned_data['mobile'])
            employer.save()
            return HttpResponseRedirect('/login/')  



        else:
                return render_to_response('Sign_up_Employer.html',{'form':form}, context_instance=RequestContext(request))


    else:
    #user is not submitting show them the registeration form
            form= SignUpForm()
            context = {'form':form}
            return render_to_response('Sign_up_Employer.html',context,context_instance=RequestContext(request))

Sign_Up_Employer.html

{%block content%}
<form action ="" method ="post">
{% csrf_token %}
{% if form.errors %}<p>Please Correct </p> {% endif %}
<div class ="register_div">
{%if form.username.errors%}<p class= "error">{{form.username.errors}}</p>{% endif %}
 <p> <label for ="username"{% if form.username.errors %} class= "error"{% endif %}> Username</label></p>
<p>{{form.username}}</p>
 </div>
 <div class ="register_div">
{%if form.first_name.errors%}<p class= "error">{{form.first_name.errors}}</p>{% endif %}
 <p> <label for ="first_name"{% if form.first_name.errors %} class= "error"{%endif%}>First name</label></p>
<p>{{form.first_name}}</p>
 </div>
 <div class ="register_div">
{%if form.last_name.errors%}<p class= "error">{{form.last_name.errors}}</p>{% endif %}
 <p> <label for ="last_name"{% if form.last_name.errors %} class= "error"{%endif%}>Last name</label></p>
<p>{{form.last_name}}</p>
 </div>
<div class ="register_div">
{%if form.address.errors%}<p class= "error">{{form.address.errors}}</p>{% endif %}
 <p> <label for ="address"{% if form.address.errors %} class= "error"{%endif%}>Address'</label></p>
<p>{{form.address}}</p>
 </div>
 <div class ="register_div">
{%if form.email.errors%}<p class= "error">{{form.email.errors}}</p>{% endif %}
 <p> <label for ="email"{% if form.email.errors %} class= "error"{%endif%}>Email Address</label></p>
<p>{{form.email}}</p>
 </div>
 <div class ="register_div">
{%if form.password.errors%}<p class= "error">{{form.password.errors}}</p>{% endif %}
 <p> <label for ="password"{% if form.password.errors %} class= "error"{%endif%}>Password</label></p>
<p>{{form.password}}</p>
 </div>

 <div class ="register_div">
{%if form.password1.errors%}<p class= "error">{{form.password1.errors}}</p>{% endif %}
 <p> <label for ="password1"{% if form.password1.errors %} class= "error"{%endif%}>Verify Password</label></p>
<p>{{form.password1}}</p>
 </div>

 <div class ="register_div">
{%if form.mobile.errors%}<p class= "error">{{form.mobile.errors}}</p>{% endif %}
 <p> <label for ="mobile"{% if form.mobile.errors %} class= "error"{%endif%}>Mobile</label></p>
<p>{{form.mobile}}</p>
 </div>
 <p><input type =submit alt =register></p>
</form>
{% endblock %}
4

2 回答 2

1

我不知道这是否只是一个剪切和粘贴问题(尽管因为它在您的另一个(已删除)问题中是相同的,我倾向于不这么认为),但是您的两种clean方法的缩进是错误的。它们目前根本不是 ModelForm 类的一部分,因此不会被调用。将它们缩进一级,因此它们与class Meta.

于 2012-04-19T18:24:29.780 回答
0

我猜在 HTML 模板中你没有将method表单的属性设置为POST 例如

<form method="post">
于 2012-04-19T17:55:33.163 回答