django - 为什么不显示 ValidationError?
问题描述
我刚开始学习 Django,但我的浏览器中没有显示 ValidationError 问题。
视图.py
from django.shortcuts import render
from .forms import ProductForm, RawProductForm
from .models import Product
def product_create_view(request):
form = ProductForm(request.POST or None)
if form.is_valid():
form.save()
form = ProductForm()
context = {
'form': form
}
return render(request, "products/product_create.html", context)
表格.py
def class_title(self, *args, **kwargs):
title = self.cleaned_data.get('title')
if "abc" in title:
return title
else:
raise forms.ValidationError("This is not a valid title")
模板.html
{% extends 'base.html' %}
{% block content %}
<form action="." method="POST">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Save" />
</form>
{% endblock %}
解决方案
推荐阅读
- html - 如何在 Bootstrap 进度条中添加带有工具提示的点?
- php - 不显示来自 Laravel Eloquent 关系的数据
- sql - 函数 pgAdmin 中的 LIKE 运算符
- laravel - 达到速率限制时,Laravel 暂停调度程序
- php - 如何使用mysql在php中上传多个文件
- python - 错误:“无法确定关系的真值”但没有使用任何操作来处理 sympy?
- scala - 在 Scala 中利用泛型返回类型
- json - 如何修复 TypeError: Object of type bytes is not JSON serializable 仅在 Docker 容器中体现?
- node.js - 如何将原始 PostgreSQL 函数添加到 TypeORM 中的查询构建器连接?
- apache-spark - Spark结构化流式传输:从最新版本/快照或增量表开始