from django import forms
from .models import Article
class ArticleForm(forms.ModelForm):
class Meta:
model = Article
fields = ['title', 'content']
from .forms import ArticleForm
def create_article(request):
if request.method == 'POST':
form = ArticleForm(request.POST)
if form.is_valid():
form.save()
return redirect('article_list')
else:
form = ArticleForm()
return render(request, 'create_article.html', {'form': form})
- ํ
ํ๋ฆฟ์์ ํผ ๋ ๋๋ง:
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">์ ์ฅ</button>
</form>
๐ฅ๏ธ ์ฅ๊ณ view.py ํจ์ ์์ฑ
- request๋ฅผ ์ธ์(ํ๋ผ๋ฏธํฐ)๋ก ๋ฐ์
- method ๊ตฌ๋ถ
- ์ฌ์ฉ์ ํ๋ฉด ๊ฒฐ์