【问题标题】:Django form validation, how to show messages on the fields?Django 表单验证,如何在字段上显示消息?
【发布时间】:2016-10-07 13:52:18
【问题描述】:

我正在构建一个注册表单,但我在验证它时遇到了一些问题。

我希望看到错误消息显示在该字段中,但我在浏览器上收到错误消息:

The User could not be created because the data didn't validate.

Request Method:     POST
Request URL:    http://127.0.0.1:8000/account/register/
Django Version:     1.9.8
Exception Type:     ValueError
Exception Value:    

The User could not be created because the data didn't validate.

Exception Location:     C:\Python34\lib\site-packages\django\forms\models.py in save, line 446

这是我的forms.py

class UserRegistrationForm(forms.ModelForm):
    password = forms.CharField(label='Password', required=False ,widget=forms.PasswordInput)
    password2 = forms.CharField(label='Repeat password', required=False  ,widget=forms.PasswordInput)

    class Meta:
        model = User
        fields = ('username', 'first_name', 'email')

    def clean_password2(self):
        password1 = self.cleaned_data.get('password1')
        password2 = self.cleaned_data.get('password2')
        #cd = self.cleaned_data
        if not password2:
            raise forms.ValidationError("Fill out the password2 .")
        if password1 != password2:
            raise forms.ValidationError("The two password fields didn't match.")
        return password2

这是我的视图寄存器

def register(request):
    if request.method == 'POST':
        user_form = UserRegistrationForm(request.POST)
        if  user_form.is_valid:
            new_user = user_form.save(commit=False)
            new_user.set_password(user_form.cleaned_data['password'])
            new_user.save()
            return render(request, 'account/register_done.html', {'new_user': new_user})
        else:
            print (user_form.errors)
    else:
        user_form = UserRegistrationForm()
    return render(request, 'account/register.html', {'user_form': user_form})

我的 htmls - register.html

{% extends "account/base.html" %}
{% block title %}Create an account{% endblock %}
{% block content %}
    <h1>Create an account</h1>
    <p>Please, sign up using the following form:</p>
    <form action="." method="post">
        {{ user_form.as_p }}
        {% csrf_token %}
        <p><input type="submit" value="Create my account"></p>
    </form>
{% endblock %}

register_done.html

{% extends "account/base.html" %}
{% block title %}Welcome{% endblock %}
{% block content %}
    <h1>Welcome {{ new_user.first_name }}!</h1>
    <p>Your account has been successfully created. Now you can <a href="{% url "login" %}">log in</a>.</p>
{% endblock %}

【问题讨论】:

  • 不知道你为什么要重新发明轮子,你可以使用django-redux包
  • 我正在关注一本书,我是从django开始的,所以在完成这本书之后我会去打包。
  • 听起来不错,你在关注哪本书?
  • 这本书就是link。我强烈推荐它。我经历了“Tango with Django”,这是一个很好的开始。

标签: django django-forms django-views


【解决方案1】:

好像你没有调用 is_valid 方法,这可能会导致这个问题:

if  user_form.is_valid 

尝试将上面的行改为:

if user_form.is_valid()

【讨论】:

  • 谢谢哥们!我真的错过了!
猜你喜欢
  • 2020-11-16
  • 2018-05-30
  • 1970-01-01
  • 2012-02-15
  • 2013-07-12
  • 2013-08-18
  • 1970-01-01
  • 2014-06-22
  • 1970-01-01
相关资源
最近更新 更多