【问题标题】:Django - show error (ValidationError) inside the texfield of the formDjango - 在表单的 texfield 内显示错误(ValidationError)
【发布时间】:2016-05-09 18:22:20
【问题描述】:

如何将错误值设置到文本字段中,现在它只显示字段上方的错误,我想在我的文本字段中显示它

实际展示:

我的目标:

forms.py

from django import forms            
from django.contrib.auth.models import User   
#from models import StudentRegistration
from django.forms import ModelForm
from promSpace.models import Space
from StudentUsers.models import StudentRegistration
from django.contrib.auth.forms import UserCreationForm  



class MyRegistrationForm(forms.ModelForm):
    password = forms.CharField(widget=forms.PasswordInput)
    password2 = forms.CharField(widget=forms.PasswordInput)
    prom_code = forms.CharField(max_length = 8)
    email = forms.EmailField(max_length=50)


    def clean_email(self):
        email = self.cleaned_data['email']
        if StudentRegistration.objects.filter(email = email).exists():
            raise forms.ValidationError("Email already exists")
        return email


    def clean_password2(self):
        password = self.cleaned_data.get('password')
        password2 = self.cleaned_data.get('password2')
        if password and password2:
            if password != password2:
                raise forms.ValidationError(("The two password fields didn't match."))
            elif password == '' or password2 == '':
                raise forms.ValidationError(("The password cannot be blank"))
        return password2


    def clean_prom_code(self):
        prom_code = self.cleaned_data['prom_code']
        prom_code_ver = Space.objects.filter(prom_code = prom_code)
        if prom_code_ver.exists():
            return prom_code
            StudentRegistration.prom_name = prom_code_ver.prom_name
        else:
            raise forms.ValidationError(("The Prom Code Does not exist"))


    class Meta:
       model = StudentRegistration
       fields = ('email', 'first_name', 'last_name','gender','prom_code','password')

register.html

<!DOCTYPE html>

<html>

    <head>
    <title></title>
    </head>


<body>

    <h1>Register</h1>
    <form action="/register/" method="post">

    {% csrf_token %}
    <div>{{form.as_p}}</div>

    <input type="submit" value="Register" />

    </form>

    </body>

我真的不知道如何开始,我一直在查看 django 文档,我唯一能找到的是 {{ form.field.value|default_if_none:"" }}

【问题讨论】:

  • 请解释“在文本字段中显示错误信息”?你的意思是你想显示一个错误而不是用户输入的内容?因此,如果用户输入了 100 个字符并犯了一个小错误,他将不得不重新输入一遍。这就是你所追求的吗?
  • 例如,我们知道仅在 html 上使用 {{form}} 就会引发 form.validationError("Email already exists") 如果是这样的话。我想要做的是在电子邮件文本字段中显示错误“电子邮件已存在”。
  • 就像一个注册表单,如果某些字段不正确,则会引发validationError,并将其显示在文本字段中

标签: python django python-2.7 templates django-forms


【解决方案1】:

您在寻找 Flash 消息系统吗?您可以在视图中使用以下内容显示 Flash 消息:

messages.error(request,"Changes were not saved.")

在模板中:

{% if messages %}
<ul class="messages">
{% for message in messages %}
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }} /li>
{% endfor %}
</ul>
{% endif %}

【讨论】:

  • 其实他正在尝试修改这个系统。
  • 他没有显示为列表项,而是尝试将 {{message}} 作为输入文本字段值传递
  • 这正是我要找的,现在我只想在 textField 中显示错误消息。
猜你喜欢
  • 1970-01-01
  • 2014-04-23
  • 2010-09-20
  • 1970-01-01
  • 2015-01-07
  • 2016-02-24
  • 2016-03-18
  • 2016-07-12
  • 1970-01-01
相关资源
最近更新 更多