Django 在我的 web-site 中没有显示 ValidationError
Django doesn't show ValidationError in my web-site
伙计们,我是 Django 的初学者。我在 youtube 上观看课程并得到不同的结果。我使用 Django==2.0.7 和 Python==3.6.5.
如果我写的标题名称不正确,我试图在我的页面上出现错误,但我不明白。看看 func - **def clean_title(self, *args, kwargs),我希望你明白我的意思。我有“raise forms.ValidationError("Error")”,但它不再起作用了。
forms.py
from django import forms
from .models import Product
class ProductForm(forms.ModelForm):
title = forms.CharField(label='',
widget=forms.TextInput(attrs={"placeholder": "title"}))
Description = forms.CharField(
required=False,
widget=forms.Textarea(
attrs={
"placeholder": "Your description",
"class": "new-class-name two",
"id": "new-class-name two",
"rows": 20,
'cols':120
}
)
)
Price = forms.DecimalField(initial=199.99)
class Meta:
model = Product
fields = [
'title',
'Description',
'Price'
]
def clean_title(self, *args, **kwargs):
title = self.cleaned_data.get('title')
if "Ruslan" in title:
return title
else:
raise forms.ValidationError("Error")
在 forms.py 中,我创建了 class ProductForm 并声明了字段。在我的页面上,我看到了。
我还定义了 clean_title。如果我填写错误的标题名称,我想得到错误。
views.py
from django.shortcuts import render
from .models import Product
from .forms import ProductForm as CreateForm, PureDjangoForm
def create_form(request):
form = CreateForm(request.POST or None)
if form.is_valid():
form.save()
form = CreateForm()
context = {
'form': form
}
return render(request, "create_product_form.html", context)
create_product_form.html
{% extends 'base.html' %}
{% block content %}
<form action='.' method="POST"> {% csrf_token %}
{{ form.as_p }}
<input type='submit' value='Save' />
</form>
{% endblock %}
这是我的 file.html,它继承了 base.html 的任何不重要的细节。
伙计们,怎么了,请帮帮我,我不明白如果标题名称不正确怎么会出错?我在我的页面上看到了所有字段并且我可以填写它,但它没有向我显示错误。
如果您创建的表单无效,您应该不创建一个新表单,因此视图应该如下所示:
def create_form(request):
if <strong>request.method == 'POST'</strong>:
form = CreateForm(request.POST, request.FILES)
if form.is_valid():
form.save()
return redirect('<em>name-of-some-view</em>')
else:
<strong>form = CreateForm()</strong>
context = {
'form': form
}
return render(request, 'create_product_form.html', context)
Note: In case of a successful POST request, you should make a redirect
[Django-doc]
to implement the Post/Redirect/Get pattern [wiki].
This avoids that you make the same POST request when the user refreshes the
browser.
伙计们,我是 Django 的初学者。我在 youtube 上观看课程并得到不同的结果。我使用 Django==2.0.7 和 Python==3.6.5.
如果我写的标题名称不正确,我试图在我的页面上出现错误,但我不明白。看看 func - **def clean_title(self, *args, kwargs),我希望你明白我的意思。我有“raise forms.ValidationError("Error")”,但它不再起作用了。
forms.py
from django import forms
from .models import Product
class ProductForm(forms.ModelForm):
title = forms.CharField(label='',
widget=forms.TextInput(attrs={"placeholder": "title"}))
Description = forms.CharField(
required=False,
widget=forms.Textarea(
attrs={
"placeholder": "Your description",
"class": "new-class-name two",
"id": "new-class-name two",
"rows": 20,
'cols':120
}
)
)
Price = forms.DecimalField(initial=199.99)
class Meta:
model = Product
fields = [
'title',
'Description',
'Price'
]
def clean_title(self, *args, **kwargs):
title = self.cleaned_data.get('title')
if "Ruslan" in title:
return title
else:
raise forms.ValidationError("Error")
在 forms.py 中,我创建了 class ProductForm 并声明了字段。在我的页面上,我看到了。 我还定义了 clean_title。如果我填写错误的标题名称,我想得到错误。
views.py
from django.shortcuts import render
from .models import Product
from .forms import ProductForm as CreateForm, PureDjangoForm
def create_form(request):
form = CreateForm(request.POST or None)
if form.is_valid():
form.save()
form = CreateForm()
context = {
'form': form
}
return render(request, "create_product_form.html", context)
create_product_form.html
{% extends 'base.html' %}
{% block content %}
<form action='.' method="POST"> {% csrf_token %}
{{ form.as_p }}
<input type='submit' value='Save' />
</form>
{% endblock %}
这是我的 file.html,它继承了 base.html 的任何不重要的细节。
伙计们,怎么了,请帮帮我,我不明白如果标题名称不正确怎么会出错?我在我的页面上看到了所有字段并且我可以填写它,但它没有向我显示错误。
如果您创建的表单无效,您应该不创建一个新表单,因此视图应该如下所示:
def create_form(request):
if <strong>request.method == 'POST'</strong>:
form = CreateForm(request.POST, request.FILES)
if form.is_valid():
form.save()
return redirect('<em>name-of-some-view</em>')
else:
<strong>form = CreateForm()</strong>
context = {
'form': form
}
return render(request, 'create_product_form.html', context)
Note: In case of a successful POST request, you should make a
redirect
[Django-doc] to implement the Post/Redirect/Get pattern [wiki]. This avoids that you make the same POST request when the user refreshes the browser.