首页 > 解决方案 > 为什么不显示 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 %}

标签: djangovalidationerror

解决方案


推荐阅读