반응형
CodingEntrepreneurs Django 강의 정리
forms>
from django import forms
from .models import BlogPost
class BlogPostForm(forms.Form):
title = forms.CharField()
slug = forms.SlugField()
content = forms.CharField(widget=forms.Textarea)
class BlogPostModelForm(forms.ModelForm):
# title = forms.CharField()
class Meta:
model = BlogPost
fields = ["title", "slug", "content"]
def clean_title(self, *args, **kwargs):
# print(dir(self))
instance = self.instance
print(instance)
title = self.cleaned_data.get("title")
qs = BlogPost.objects.filter(title=title)
if instance is not None:
qs = qs.exclude(pk=instance.pk) # id=instance.id
if qs.exists():
raise forms.ValidationError(
"This title has already been used. Please try again."
)
return title
반응형
'Python_WEB > Try_Django' 카테고리의 다른 글
[Django]Blog Post Navigation (0) | 2020.06.21 |
---|---|
[Django]Delete and Confirm (0) | 2020.06.21 |
[Django]Update View with Model Form (0) | 2020.06.21 |
[Django]Logged In user & Forms (0) | 2020.06.21 |
[Django]Associate Blog Post to a User with Foreign Keys (0) | 2020.06.21 |