我刚刚开始学习Django,但ValidationError出现问题,该问题未显示在浏览器中。
Views.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)
forms.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")
template.html
{% extends 'base.html' %}
{% block content %}
<form action="." method="POST">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Save" />
</form>
{% endblock %}