๐งฉ ์ํฉ
Django์์ ์ฌ์ฉ์ ์ ๋ ฅ ํผ์ ๋ง๋ค ๋ forms.Form์ ์ฐ๋ฉด ์ ํจ์ฑ ๊ฒ์ฌ๋ ํ๋ ๊ตฌ์ฑ์ด ๋ฒ๊ฑฐ๋ก์์. ๊ทธ๋์ forms.ModelForm์ ์ฌ์ฉํ๋ฉด ๋ชจ๋ธ ๊ธฐ๋ฐ์ผ๋ก ํผ์ ์๋ ์์ฑํ ์ ์์ด์ ๋งค์ฐ ๊ฐํธํฉ๋๋ค.
class ReviewForm(forms.ModelForm):
class Meta:
model = Review
fields = "__all__"
์์ ๊ฐ์ด ์ฐ๋ฉด Review ๋ชจ๋ธ์ ๋ชจ๋ ํ๋๋ฅผ ์๋์ผ๋ก ํผ์ผ๋ก ์์ฑํด์ค๋๋ค. ํ์ง๋ง ๋ฌธ์ ๋...
โ ๋ ์ด๋ธ(label)์ด ๋ค ์์ด ๋ณ์๋ช ์ผ๋ก ๋์์!

๐ ์์ธ
ModelForm์ Model ํด๋์ค์ ์ ์๋ ํ๋ ์ด๋ฆ์ ๊ทธ๋๋ก label๋ก ์ฌ์ฉํฉ๋๋ค. verbose_name์ ๋ชจ๋ธ์์ ์ ์ํ์ง ์์ผ๋ฉด ๋ณ์๋ช ์ ๊ทธ๋๋ก ์ถ๋ ฅํ์ฃ .
๐ ํด๊ฒฐ ๋ฐฉ๋ฒ: labels์ error_messages ์ค์
์๋์ฒ๋ผ Meta ํด๋์ค ์์ labels์ error_messages๋ฅผ ์ง์ ํด์ฃผ๋ฉด forms.Form ๋ชป์ง์๊ฒ ์น์ ํ UI์ ๋ฉ์์ง๋ฅผ ๋ณด์ฌ์ค ์ ์์ด์.
class ReviewForm(forms.ModelForm):
class Meta:
model = Review
fields = "__all__"
labels = {
"user_name": "Your Name",
"review_text": "Your Feedback",
"rating": "Your Rating"
}
error_messages = {
"user_name": {
"required": "Your name must not be empty!",
"max_length": "Please enter a shorter name!"
}
}
โ ๊ฒฐ๊ณผ

- ์ฌ์ฉ์์๊ฒ ๋ณด์ด๋ ๋ ์ด๋ธ์ ์น์ ํ๊ฒ ์ง์ ๊ฐ๋ฅ
- required ์กฐ๊ฑด ์๋ฐ ์ ์ฌ์ฉ์ ์นํ์ ์ธ ๋ฉ์์ง ์ ๊ณต
๐ ์์ฝ
ํญ๋ชฉ ๋ฐฉ๋ฒ
| ๋ชจ๋ธ ๊ธฐ๋ฐ ํผ ์์ฑ | forms.ModelForm |
| ๋ ์ด๋ธ ์ง์ | Meta.labels |
| ์๋ฌ ๋ฉ์์ง ์ง์ | Meta.error_messages |
๐ท ์ถ์ฒ ํ๊ทธ
#Django #ModelForm #FormValidation #PythonWebDev #์น๊ฐ๋ฐ๊ธฐ์ด