最初,验证检查有效,在这种情况下,它会生成错误消息。但是,当我将继承与mixins一起使用时,就有逻辑得到一个奇怪的错误。
Views.py
class TagCreate(LoginRequiredMixin, ObjectCreateMixin, View):
model_form = TagForm
template = 'blog/tag_create.html'
raise_exception = True
Utils.py
class ObjectCreateMixin:
model_form = None
template = None
def get(self, request):
form = self.model_form()
return render(request, self.template, context={'form': form})
def post(self, request):
bound_form = self.model_form(request.POST)
if bound_form.is_valid():
new_obj = bound_form.save()
return redirect(new_obj)
return render(request, template, context={'form':bound_form}) #Here is an error in django debugger
Forms.py
class TagForm(forms.ModelForm):
class Meta:
model = Tag
fields = ['title', 'slug']
widgets = {
'title': forms.TextInput(attrs={'class':'form-control'}),
'slug': forms.TextInput(attrs={'class':'form-control'}),
}
def clean_slug(self):
new_slug = self.cleaned_data['slug'].lower()
if new_slug == 'create':
raise ValidationError('Slug may not be "Create"')
if Tag.objects.filter(slug__iexact=new_slug).count():
raise ValidationError('Slug must be unique. We have "{}" slug already'.format(new_slug))
return new_slug