Django 不显示验证错误
Django not displaying Validationerror
我在编码 django 时遇到错误,不显示验证错误
我不知道为什么 django 不显示 validationerror 。你能帮忙吗。
这是我的form.py文件
这是我的表单文件,其中包含表单和验证。
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 lang="en" 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>
您需要将错误标记添加到模板中才能查看验证错误:
{% if form.errors %}
{% for field in form %}
{% for error in field.errors %}
<div class="alert alert-danger">
<strong>{{ error }}</strong>
</div>
{% endfor %}
{% endfor %}
{% for error in form.non_field_errors %}
<div class="alert alert-danger">
<strong>{{ error }}</strong>
</div>
{% endfor %}
{% endif %}
您还需要确保呈现原始完成的表单,以便显示错误。
def register(request):
if request.method == 'GET':
form = formss.Forms()
if request.method == 'POST':
form = formss.Forms(request.POST)
if form.is_valid():
form.save(commit=True)
return index(request)
return render (request , 'forms.html', context = {'form':form} )
我在编码 django 时遇到错误,不显示验证错误 我不知道为什么 django 不显示 validationerror 。你能帮忙吗。
这是我的form.py文件 这是我的表单文件,其中包含表单和验证。
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 lang="en" 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>
您需要将错误标记添加到模板中才能查看验证错误:
{% if form.errors %}
{% for field in form %}
{% for error in field.errors %}
<div class="alert alert-danger">
<strong>{{ error }}</strong>
</div>
{% endfor %}
{% endfor %}
{% for error in form.non_field_errors %}
<div class="alert alert-danger">
<strong>{{ error }}</strong>
</div>
{% endfor %}
{% endif %}
您还需要确保呈现原始完成的表单,以便显示错误。
def register(request):
if request.method == 'GET':
form = formss.Forms()
if request.method == 'POST':
form = formss.Forms(request.POST)
if form.is_valid():
form.save(commit=True)
return index(request)
return render (request , 'forms.html', context = {'form':form} )