【问题标题】:Display django form error for each field in template and each correct data in template为模板中的每个字段和模板中的每个正确数据显示 django 表单错误
【发布时间】:2016-12-02 18:14:29
【问题描述】:

我是 django 新手,我正在尝试验证表单数据,如果发生任何错误,请将该数据传输到模板页面,并使用旧值显示在表单中。如果可能的话,我想要字典中每个数据的错误消息

views.py

from django.shortcuts import render

from .forms import RegForm

# Create your views here.
def login(request):

    return render(request, 'login.html')

def registration(request):
    if request.method == 'POST':
        form = RegForm(request.POST)

        if form.is_valid():
            print "Form is valid"
            print form.cleaned_data

            user_data = {
                'firstname':form.cleaned_data['firstname'],
                'lastname':form.cleaned_data['secondname'],
                'username': form.cleaned_data['username'],
                'mail':form.cleaned_data['mail'],
                'password': form.cleaned_data['password']}
            print user_data
        else:
            print "Form is not valid"
            print form['mail'].errors
    return render(request, 'register.html')
   forms.py



    from django import forms

    class RegForm(forms.Form):

        firstname = forms.CharField(max_length=100)
        secondname = forms.CharField(max_length=100)
        username = forms.CharField(max_length=100, min_length=8)
        mail = forms.EmailField()
        password = forms.CharField(widget = forms.PasswordInput(), min_length=8, max_length=100)

注册.html

     <!DOCTYPE html>
    <html>
        <head>
            <title>registration</title>
            <style type="text/css">
                header{
                    width: 100%;
                    display: block;
                }
                section{
                    width: 180px;
                    margin: auto;
                    display: block;
                }
                nav{
                    width: 180px;
                    float: right;
                    display: block;
                }

            </style>
    </head>
    <body>
        <header>
            <nav>
                <a href="{% url 'registration' %}">Registration</a>
                <a href="{% url 'login' %}">Login</a>
            </nav>
        </header>
        <section>
        <h1 align="center">Register:</h1>
            <form method = "post" action = "{% url 'registration' %}">
                {% csrf_token %}
                <label>First Name:</label><br>
                <input type="text" name="firstname"><br>
                <label>Second Name:</label><br>
                <input type="text" name="secondname"><br>
                <label>Mail Id:</label><br>
                <input type="text" name="mail"><br>
                <label>User Name:</label><br>
                <input type="text" name="username" value=""><br>
                <label>Password:</label><br>
                <input type="password" name="password"><br><br>
                <input type="submit" name="submit" value="submit">
            </form>
        </section>

    </body>
</html>

【问题讨论】:

    标签: django django-forms django-templates


    【解决方案1】:

    从您的views.py,将表单对象传递给模板

     ...
    return render(request, 'register.html', { 'form': form })
     ...
    

    现在,在模板中,您可以使用对象form 进行渲染。 例如,对于您正在执行的 First Name 字段:

     ...
     <label>First Name:</label><br>
     <input type="text" name="firstname"><br>
     ...
    

    你应该这样做:

     ...
     <label for="{{ form.firstname.id_for_label }}">First Name:</label>
       {{ form.firstname }}
       {{ form.firstname.errors }}
     ...
    

    {{ form.firstname }} 为您呈现 html 输入标签,而 {{ form.firstname.errors }} 为该字段呈现错误。

    最后,您还可以使用模板标签{{ form.non_field_errors }}(通常在表单的末尾或顶部)来呈现其他错误。

    文档对此主题非常清楚。请查看rendering-fields-manually 了解更多信息。

    【讨论】:

      猜你喜欢
      • 2011-10-11
      • 2023-01-10
      • 2015-02-14
      • 2011-06-06
      • 2011-05-28
      • 2021-01-13
      • 2018-05-30
      • 2017-10-20
      相关资源
      最近更新 更多