显示模板中每个字段的django表单错误以及模板中的每个正确数据

时间:2016-07-28 11:22:09

标签: django django-forms django-templates

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

register.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>

1 个答案:

答案 0 :(得分:8)

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