๐Ÿงฉ DJANGO ํผ ๋ฐ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ

alsk9624ยท2024๋…„ 8์›” 26์ผ
0
  • ํผ ํด๋ž˜์Šค ์ •์˜:
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 ํ•จ์ˆ˜ ์ž‘์„ฑ

  1. request๋ฅผ ์ธ์ž(ํŒŒ๋ผ๋ฏธํ„ฐ)๋กœ ๋ฐ›์Œ
  2. method ๊ตฌ๋ถ„
  3. ์‚ฌ์šฉ์ž ํ™”๋ฉด ๊ฒฐ์ •
profile
์•ž๊ธธ์— ๋Ÿญํ‚คโ˜…๋น„ํ‚ค๋งŒ ์žˆ๊ธธ ,,,๐Ÿ€๐Ÿซง

0๊ฐœ์˜ ๋Œ“๊ธ€