Django ViewClass 2-3) Form

Turtleยท2023๋…„ 12์›” 7์ผ
0
post-thumbnail

๐Ÿ“ŒFormView

  • ModelForm, Form์„ ์ง€์ •ํ•ด์„œ ํผ์„ ์•Œ์•„์„œ ์ž‘์„ฑํ•ด์ฃผ๋Š” ํด๋ž˜์Šค

  • ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ ํ›„ success_url๋กœ ์ง€์ •๋œ ์ฃผ์†Œ๋กœ ์ž๋™ ๋ฆฌ๋””๋ ‰์…˜

  • template_name์œผ๋กœ ํ…œํ”Œ๋ฆฟ ์ง€์ •์ด ๊ฐ€๋Šฅ

ํผ ๋ทฐ ํด๋ž˜์Šค์˜ ๊ธฐ๋ณธ ํ˜•ํƒœ

class FormView:
	template_name: str
    form_class: cls
    success_url: str
    
    def form_valid(self, form: Form):
    	return response: HttpResponse

๐Ÿ“ŒDjango FormView

from myapp.forms import ContactForm
from django.views.generic.edit import FormView

class ContactFormView(FormView):
    template_name = "contact.html"
    form_class = ContactForm
    success_url = "/thanks/"

    def form_valid(self, form):
        # This method is called when valid form data has been POSTed.
        # It should return an HttpResponse.
        form.send_email()
        return super().form_valid(form)
  • FormView๋Š” TemplateResponseMixin์„ ์ƒ์†ํ•˜๋ฏ€๋กœ ์—ฌ๊ธฐ์„œ template_name์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

๐Ÿ“Œ์ ์šฉ

from django import forms
from .models import Task

class TaskForm(forms.ModelForm):
    class Meta:
        model = Task
        fields = ['title', 'type', 'due']
from django.http import HttpRequest, HttpResponse
from django.shortcuts import render, HttpResponseRedirect, redirect
from django.views.generic import TemplateView, CreateView, UpdateView, DeleteView, ListView, DetailView, FormView
from .models import Task, ChecklistItem
from django.utils import timezone
from django.core.paginator import Paginator
from django.urls import reverse_lazy

class TaskCreateView(FormView):
    template_name = "pages/task_create.html"
    form_class = TaskForm
    success_url = '/'
    
    def form_valid(self, form):
        form.save()
        return super().form_valid(form)
  • form_valid์˜ ํ•จ์ˆ˜ ์›ํ˜•์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.
def form_valid(self, form):
	"""If the form is valid, redirect to the supplied URL."""
    return HttpResponseRedirect(self.get_success_url())
  • form_valid()์˜ ๊ธฐ๋ณธ ๊ตฌํ˜„์€ ๋‹จ์ˆœํžˆ success_url๋กœ ๋ฆฌ๋””๋ ‰์…˜๋˜๋Š” ๊ฒƒ์ด๋‹ค.
  • ๋”ฐ๋ผ์„œ ์œ„์—์„œ ํผ์— ์˜ํ•œ ์ฒ˜๋ฆฌ๊ฐ€ ์ด๋ฃจ์–ด์ง„ ๋‹ค์Œ์— form_valid ์ฝ”๋“œ๋ฅผ ๊ทธ๋Œ€๋กœ ์žฌ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ super() ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

๐Ÿ“Œ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœํ•˜๊ธฐ: super() ์ด์šฉ

  • ๋ถ€๋ชจ ํด๋ž˜์Šค์—์„œ ๊ตฌํ˜„ํ–ˆ๋˜ ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ์˜ˆ์ œ๋ฅผ ๊ฐ„๋‹จํ•˜๊ฒŒ ์‚ดํŽด๋ณด๋ฉด์„œ super() ํ‚ค์›Œ๋“œ๊ฐ€ ๋ฌด์—‡์„ ์˜๋ฏธํ•˜๋Š”์ง€ ์•Œ์•„๋ณด์ž.
  • ์•„๋ž˜์˜ ์˜ˆ์ œ๋Š” get_name์„ ์ž์‹ ํด๋ž˜์Šค์—์„œ ์ƒˆ๋กญ๊ฒŒ ๊ตฌํ˜„ํ–ˆ์Œ์—๋„ ๋ถ€๋ชจ ํด๋ž˜์Šค์—์„œ ๊ตฌํ˜„๋˜์—ˆ๋˜ get_name์„ ํ˜ธ์ถœํ•˜๋Š” ๊ฒฝ์šฐ์ด๋‹ค.
class Student(Person):
	def __init__(self, name, age, GPA):
    	super().__init__(name, age)
        self.GPA = GPA
       
    def get_name(self):
    	print(f'์ €๋Š” ๋Œ€ํ•™์ƒ {self.name}์ž…๋‹ˆ๋‹ค.')
        
    def get_GPA(self):
    	super().get_name()	# ๋ถ€๋ชจ ํด๋ž˜์Šค(Person)์˜ get_name์„ ํ˜ธ์ถœ
        print(f'์ œ ํ•™์ ์€ {self.GPA}์ž…๋‹ˆ๋‹ค.')
  • get_GPA ํ•จ์ˆ˜ ๋‚ด์—์„œ Student ํด๋ž˜์Šค์˜ get_name์ด ์•„๋‹Œ Person ํด๋ž˜์Šค์˜ get_name์„ ํ˜ธ์ถœํ•˜๊ณ  ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ํŒŒ์•…ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ๊ทธ๋Œ€๋กœ ํ˜ธ์ถœํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด super().๋ฉ”์†Œ๋“œ์ด๋ฆ„ ํ˜•ํƒœ๋กœ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋ฉด ๋œ๋‹ค.

๐Ÿ“ŒModelForm

  • ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ธฐ๋ฐ˜์˜ ์•ฑ์„ ๊ตฌ์ถ•ํ•˜๋Š” ๊ฒฝ์šฐ Django์˜ ๋ชจ๋ธ๊ณผ ๋ฐ€์ ‘ํ•˜๊ฒŒ ๋งคํ•‘๋˜๋Š” ์–‘์‹์„ ๋งŒ๋“ค ๊ฐ€๋Šฅ์„ฑ์ด ๋†’์€๋ฐ ์ด ๋•Œ, ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด ๋ฐ”๋กœ ModelForm

  • Metaํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ชจ๋ธ์„ ์—ฐ๊ฒฐํ•˜๊ณ  ์‚ฌ์šฉ

from django.forms import ModelForm
from myapp.models import Article

class ArticleForm(ModelForm):
	class Meta:
    	model = Article
        fields = ["pub_date", "headline", "content", "reporter"]

๐Ÿ“ŒField types

  • Django์—์„œ ์ง€์›ํ•˜๋Š” Field type์€ Django document์—์„œ ์ฐพ์•„๋ณผ ์ˆ˜ ์žˆ๋‹ค. ์ž์‹ ์ด ์›ํ•˜๋Š” ๋ชจ๋ธ๊ณผ ํผ์„ ์ •ํ™•ํ•˜๊ฒŒ ์„ค๊ณ„ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ํ•„๋“œ์— ๋Œ€ํ•œ ์ง€์‹์€ ํ•„์ˆ˜์ด๋ฏ€๋กœ ์ฐธ๊ณ ํ•ด์„œ ์ •ํ™•ํ•˜๊ฒŒ ์ž‘์„ฑํ•˜๋ฉด ๋œ๋‹ค.

์ฐธ๊ณ ์ž๋ฃŒ - Django Creating forms from models

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