[Django] - 7. Form ์‚ฌ์šฉํ•˜๊ธฐ

์ตœ์ฐฝ์šฐยท2022๋…„ 9์›” 8์ผ
0

Django

๋ชฉ๋ก ๋ณด๊ธฐ
8/10
post-thumbnail

๐Ÿ“œ Form ์‚ฌ์šฉํ•˜๋Š”๋ฒ•์„ ์•Œ์•„๋ณด์ž.

๐Ÿ“• Form ์‚ฌ์šฉํ•˜๊ธฐ

๐Ÿ“– Form ์ด๋ž€?

Form์€ ์‚ฌ์šฉ์ž์˜ ์ž…๋ ฅ์„ ๋ฐ›๊ธฐ ์œ„ํ•œ ํ•„๋“œ๋‚˜ ์œ„์ ฏ๋“ค์˜ ๋ฌถ์Œ์„ ์˜๋ฏธ

  • ์›นํŽ˜์ด์ง€ ๋กœ๊ทธ์ธํ™”๋ฉด์— IP/PW ์ž…๋ ฅ ์นธ๊ณผ ํ™•์ธ ๋ฒ„ํŠผ ๋“ฑ์˜ ๋ฌถ์Œ์„ ์˜๋ฏธ

HTML Form์˜ ๊ธฐ๋ณธํ˜•ํƒœ

action : ์•„๋ž˜ submit์„ ๋ˆŒ๋ €์„๋•Œ ์ž‘์„ฑํ•œ ๋‚ด์šฉ์ด ๋„˜์–ด๊ฐˆ url๊ฒฝ๋กœ
type="text" : ํ…์ŠคํŠธ ์ž…๋ ฅํ•˜๋Š” ๊ณณ
type="submit" : ๋‹ค์Œ action์„ ์ทจํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋˜๋Š” ๋ฒ„ํŠผ

<form action="๋ฐ์ดํ„ฐ๊ฐ€ ์ „๋‹ฌ๋  ์ฃผ์†Œ(url)" method="HTTP method ์ค‘ ํ•˜๋‚˜">
  <input type="text" name="๋‚ด์šฉ ์ž…๋ ฅ๋ž€"/>
  <button type="submit">ํ™•์ธ</button>
</form>

Django์—์„œ๋Š” ์ด๋Ÿฌํ•œ ํผ์„ ๊ตฌ์„ฑํ•˜๊ธฐ ์‰ฝ๊ฒŒ ์ง€์›ํ•ด์คŒ.

๐Ÿ“– Form ์ƒ์„ฑ ๋ฐ ์‹คํ–‰ํ•˜๊ธฐ

app_2th/forms.py

from django import forms

class Write_Form(forms.Form):
    # forms.@ : ์–ด๋–ค ํ˜•ํƒœ์˜ ์ž…๋ ฅ์„ ๋ฐ›์„ ๊ฒƒ์ธ์ง€ ์ •์˜
    # label, max_length, widget : ํ•ด๋‹น ํ•„๋“œ์˜ ์†์„ฑ
    first_name = forms.CharField(label="์ด๋ฆ„",max_length=50)
    last_name = forms.CharField(label="์„ฑ",max_length=50)

app_2th/urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('person/',views.person,name="person"),
    path('write/',views.write,name='write'),

app_2th/views.py

from django.shortcuts import render
from .models import Person
from .forms import *

def write(request):
    form = Write_Form() # forms.py ์—์„œ ์ •์˜ํ•œ ํด๋ž˜์Šค๋ฅผ ๊ฐ์ฒด๋กœ ๋„˜๊ฒจ์คŒ
    
    # context๋ฅผ render๋กœ ๋„˜๊ฒจ์ค„ ๋•Œ, ํ•ญ์ƒ ๋”•์…”๋„ˆ๋ฆฌํ˜•ํƒœ์—ฌ์•ผํ•จ.
    context = {
        'form':form
    }
    return render(request,'write.html',context)

app_2th/templates/write.html

<form action="/" method="post"> 
    {% csrf_token %}
    {{ form }}
    <button type="submit">์ œ์ถœ</button>
</form>

์‹คํ–‰๊ฒฐ๊ณผ

๐Ÿ“– Form ์— ์ž‘์„ฑํ•œ ๋ฐ์ดํ„ฐ ๋„˜๊ธฐ๊ธฐ

Form์— ์ž‘์„ฑํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๋„˜๊ธฐ๋ ค๋ฉด ํ•ด์ฃผ์–ด์•ผ ํ•  ๊ฒƒ.

  1. ์œ„์—์„œ ์ž‘์„ฑํ•œ write.html ์—์„œ action ๋ถ€๋ถ„์— ๊ฒฝ๋กœ๋ฅผ ์ง€์ •
<form action="{% url โ€˜resultโ€™ %}" method="post"> 
    {% csrf_token %}
    {{ form }}
    <button type="submit">์ œ์ถœ</button>
</form>
  1. app/urls.py ์ถ”๊ฐ€
from django.urls import path
from . import views

urlpatterns = [
    path('person/',views.person,name="person"),
    path('write/',views.write,name='write'),
    path('result/,views.result,name='result'), # ์ถ”๊ฐ€๋œ ํ•ญ๋ชฉ 
  1. app/views.py ์ถ”๊ฐ€
def result(request):
    print(request) # <WSGIRequest: POST '/result/'>
    print(request.POST) #  <QueryDict: {'csrfmiddlewaretoken': ['L1yF1GyP6kwLvt86YEp4yZxivIN05BPvHRSlZsrob0dE7wnxR3GvPivDySWo7Lr7'], 
                        #               'first_name': ['changwoo'], 
                        #               'last_name': ['choi']}>
    form  = Write_Form(request.POST)
    context = {
        'form':form
    }
    if form.is_valid():
        return render(request,'result.html',context)    
    return HttpResponseRedirect('/write/')
  1. result.html ์ƒ์„ฑ
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>User confirmation</title>
</head>
<body>
    <h4>{{ form.first_name.value }}</h4>
    <h4>{{ form.last_name.value }}</h4>
</body>
</html>

๊ฒฐ๊ณผ

profile
์œ ๋Šฅํ•œ ๊ฐœ๋ฐœ์ž๊ฐ€ ๋˜๊ณ  ์‹ถ์€ ํ—ฌ๋ฆฐ์ด

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