Django,密码不匹配错误消息不显示。自定义UserAuth应用程序

时间:2015-10-20 21:52:47

标签: django python-3.x django-authentication

我遇到错误的问题"没有提出"在注册表单页面内。它似乎再次拉动表格,但没有"密码不匹配"消息。

我的表单处理程序代码

from django import forms 

from accounts.models import User

class RegistrationForm(forms.ModelForm):
    """
    Form for registering a new account.
"""
email = forms.EmailField(widget=forms.TextInput,label="Email")
password1 = forms.CharField(widget=forms.PasswordInput,
                            label="Password")
password2 = forms.CharField(widget=forms.PasswordInput,
                            label="Password (again)")

class Meta:
    model = User
    fields = ['email', 'password1', 'password2']

def clean(self):
    """
    Verifies that the values entered into the password fields match

    NOTE: Errors here will appear in ``non_field_errors()`` because it applies to more than one field.
    """
    cleaned_data = super(RegistrationForm, 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("Passwords don't match. Please enter both fields again.")
    return self.cleaned_data

def save(self, commit=True):
    user = super(RegistrationForm, self).save(commit=False)
    user.set_password(self.cleaned_data['password1'])
    if commit:
        user.save()
    return user

我的观点代码:

def register(request):
"""
User registration view.
"""
if request.method == 'POST':
    form = RegistrationForm(data=request.POST)
    if form.is_valid():
        user = form.save()
        return redirect('/')
else:
    form = RegistrationForm()
return render_to_response('accounts/register.html', {
    'form': form,
}, context_instance=RequestContext(request))

我将注册设置为应用的根目录' /'

帐户应用中的模式

来自django.conf.urls导入网址    来自帐户导入视图

 urlpatterns = [
  url(r'^$', views.register, name='register'),
  #url(r'^register$', views.register, name='register'),
  url(r'^login$', views.login, name='login'),
  url(r'^logout$', views.logout, name='logout'),

]

在main中:

urlpatterns = [
    url(r'^accounts/', include('accounts.urls', namespace='accounts')),

    # sets login/register to root url
    url(r'^', include('accounts.urls', namespace ='accounts')),
    url(r'^admin/', include(admin.site.urls))

帐户应用将用户名设置为userAuth中内置的djangos的电子邮件。所有其他字段填写在html页面上,例如"无效的电子邮件地址"

我已经尝试了多项但到目前为止都失败了,在这个阶段会得到任何帮助。

谢谢。

我使用的是ubuntu 14,python 3.4 django 1.8,virtualenv

1 个答案:

答案 0 :(得分:0)

您正在使用:

raise forms.ValidationError("Passwords don't match...')

所以这是一个全局错误;它不会显示在特定字段旁边。确保模板accounts/register.html显示这些错误。

也只是为了确保;您可以在print之前raise(仅用于调试),这样您就可以确认首先进行了验证。