【问题标题】:Django , password dont match error message not displaying. Customised UserAuth appDjango,密码不匹配错误信息不显示。自定义用户身份验证应用
【发布时间】:2016-01-19 18:01:40
【问题描述】:

我遇到了在注册表单页面中“未提出”错误的问题。似乎再次拉出表单,但没有“密码不匹配”消息。

我的表单处理程序代码

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 导入 url 从帐户导入视图

 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'),

]

主要:

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))

]

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

我已经尝试了多种方法,但到目前为止都失败了,现阶段如有任何帮助,我们将不胜感激。

谢谢。

我正在使用 ubuntu 14、python 3.4 django 1.8、virtualenv

【问题讨论】:

  • 澄清一下,当你用两个不匹配的密码发布表单时,Django 仍然成功注册用户?或者它没有注册用户,重定向到'/'并且也没有引发任何错误?
  • 没有发生注册...

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


【解决方案1】:

您正在使用:

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

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


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

【讨论】:

    【解决方案2】:

    使用此代码注册/signup.html

    {% if form.errors %}
    {% for field in form %}
    {% for error in field.errors %}
     <div class="alert alert-danger">
     <strong>{{ error|escape }}</strong>
     </div>
    {% endfor %}
    {% endfor %}
    {% for error in form.non_field_errors %}
     <div class="alert alert-danger">
     <strong>{{ error|escape }}</strong>
     </div>
    {% endfor %}
    {% endif %}
    

    【讨论】:

      猜你喜欢
      • 2020-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-23
      • 2016-01-20
      • 1970-01-01
      • 2021-06-30
      • 2021-08-12
      相关资源
      最近更新 更多