无法将用户添加到数据库

时间:2012-04-19 17:50:01

标签: python django

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

forms.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

view.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 %}

2 个答案:

答案 0 :(得分:1)

我不知道这是否只是一个剪切和粘贴的问题(虽然它在你的其他(已删除的)问题中是相同的,但我倾向于不考虑),但是你的两个缩进{ {1}}方法是错误的。它们目前根本不是ModelForm类的一部分,因此不会被调用。将它们缩进一级,因此它们与clean处于同一级别。

答案 1 :(得分:0)

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

<form method="post">