Django 不显示验证错误

我在编码 django 时遇到错误问题,不显示验证错误 我不知道为什么 django 不显示验证错误。你能帮忙吗?


这是我的 form.py 文件, 这是我的表单文件,其中包含 form 和 validation 。


django import forms

from django.core import validators

from new_app.models import Names``

from django.shortcuts import render

from django.http import HttpResponse




class Forms(forms.ModelForm):



FirstName = forms.CharField(label='FirstName',widget=forms.TextInput(attrs={"placeholder":"your Name","rows":20,"column":100}))


    class Meta():

        model = Names

        fields = '__all__'


    def clean(self):


        all_data = super().clean()

        firstname= all_data['FirstName']

        lastname = all_data['LastName']

        a = 1

        if firstname in lastname:

                raise forms.ValidationError("amazing")

        return all_data

这是我的 view.py 文件 这是我的视图文件


from django.shortcuts import render

from new_app.models import Names

from new_app import formss

from django import forms


# Create your views here.

def index(request):

    a = Names.objects.all()

    dict = {'write':a}

return render(request , 'index.html', context = dict)

def 寄存器(请求):


second = formss.Forms()



if request.method == 'POST':

    form = formss.Forms(request.POST)


    if form.is_valid():

        form.save(commit=True)

        return index(request)

    else:

        second = formss.Forms()







return render (request , 'forms.html', context = {'form':second} )

这是我的 form.html 模板文件...


  <!DOCTYPE html>

<html dir="ltr">

  <head>

    <meta charset="utf-8">

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">


    <title>Login or Sign up</title>

  </head>

  <body>

    

    <div class="container">

      <div class="jumbotron">

        <h1>         Sign up</h1>

      </div>


    </div>


    <div class="container">



      <div class="first">

        <form class="form-horizontal" method="post">

          {{form.as_p}}


          {% csrf_token%}

          <input type="submit" class="btn btn-primary" value="Submit">

        </form>



      </div>

    </div>


  </body>

</html>


qq_花开花谢_0
浏览 127回答 1
1回答

鸿蒙传说

您需要将错误标签添加到模板中才能查看验证错误:{% if form.errors %}&nbsp; &nbsp; &nbsp; &nbsp;{% for field in form %}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {% for error in field.errors %}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="alert alert-danger">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <strong>{{ error }}</strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {% endfor %}&nbsp; &nbsp; &nbsp; &nbsp;{% endfor %}&nbsp; &nbsp; &nbsp; &nbsp;{% for error in form.non_field_errors %}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="alert alert-danger">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <strong>{{ error }}</strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp;{% endfor %}{% endif %}您还需要确保呈现原始完成的表单,以便显示错误。def register(request):&nbsp; &nbsp; if request.method == 'GET':&nbsp; &nbsp; &nbsp; &nbsp; form = formss.Forms()&nbsp; &nbsp; if request.method == 'POST':&nbsp; &nbsp; &nbsp; &nbsp; form = formss.Forms(request.POST)&nbsp; &nbsp; &nbsp; &nbsp; if form.is_valid():&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; form.save(commit=True)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return index(request)&nbsp; &nbsp; return render (request , 'forms.html', context = {'form':form} )
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python