Почему не отображается ValidationError? - PullRequest
0 голосов
/ 26 мая 2019

Я только начал изучать 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)

форм.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 %}
...