반응형
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):
title = self.cleaned_data.get("title")
qs = BlogPost.objects.filter(title=title)
if qs.exists():
raise forms.ValidationError(
"This title has already been used. Please try again."
)
return title
forms>
from django import forms
class ContactForm(forms.Form):
full_name = forms.CharField()
email = forms.EmailField()
content = forms.CharField(widget=forms.Textarea)
def clean_email(self, *args, **kwargs):
email = self.cleaned_data.get("email")
print(email)
if email.endswith(".edu"):
raise forms.ValidationError(
"This is not a valid email. Please don't use. edu."
)
return email
반응형
'Python_WEB > Try_Django' 카테고리의 다른 글
[Django]Associate Blog Post to a User with Foreign Keys (0) | 2020.06.21 |
---|---|
[Django]Login Required (0) | 2020.06.21 |
[Django]Model Form (0) | 2020.06.21 |
[Django]Saving Data from a Django Form (0) | 2020.06.17 |
[Django]A Django Form (0) | 2020.06.17 |