[django] #4 Django๋กœ CRUDํ•˜๊ธฐ ๐Ÿ“ฅ๐Ÿ‘€โœ๏ธ๐Ÿ“ค

โœจNew Wisdomโœจยท2020๋…„ 8์›” 24์ผ
1

๐Ÿ“˜Django๐Ÿ“˜

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

CRUD

์ด ์ชฝ ์ „๊ณต ํ–ˆ๋‹ค๋ฏ„... CRUD ํ•œ ๋ฒˆ์ฏค ๋“ค์–ด๋ดค์„๊ฑฐ์œ ...
๐Ÿ“ฅ Create : ๋ฐ์ดํ„ฐ ์ƒ์„ฑ
๐Ÿ‘€ Read : ๋ฐ์ดํ„ฐ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
โœ๏ธ Update : ๋ฐ์ดํ„ฐ ์ˆ˜์ • / ๊ฐฑ์‹ 
๐Ÿ“ค Delete : ๋ฐ์ดํ„ฐ ์‚ญ์ œํ•˜๊ธฐ

์ž ๊ทธ๋Ÿผ ์ €๋ฒˆ์— ์ด์–ด์„œ ๋ณธ๊ฒฉ์ ์œผ๋กœ CRUD ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•œ๋‹ค!

์ด๊ฑฐ ๊ตฌ๊ธ€๋งํ•˜๋‹ค ๋ฐœ๊ฒฌํ•œ ์ƒˆ์ธ๋ฐ ์ฉœ ๋งˆ์Œ์— ๋“œ๋„คใ…Ž์ž์ฃผ ๋“ฑ์žฅํ•  ์˜ˆ์ •๐Ÿ’™

๐Ÿšฉ Template

๋‚˜๋Š” Review๋ฅผ ์“ฐ๊ณ , ์ฝ๊ณ , ์ˆ˜์ •ํ•˜๊ณ , ์‚ญ์ œํ•˜๋Š” ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ• ๊ฑฐ๋‹ค.
๐Ÿ“ƒ create.html (๋ฆฌ๋ทฐ ์“ฐ๊ธฐ)

<body>
    <h1>๋ฆฌ๋ทฐ ์“ฐ๊ธฐ</h1>
    <form method="POST" action=''>
        {%csrf_token%}
        {{form.as_p}}
        <input type = 'submit' value = '๋“ฑ๋กํ•˜๊ธฐ'/>
    </form>
</body>
  • {% csrf_token %} : django์—์„œ post ๋ฐฉ์‹์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ „์†กํ•  ๋•Œ ๊ธฐ๋ณธ์œผ๋กœ ์ œ๊ณตํ•˜๋Š” ๋ณด์•ˆ์ˆ˜๋‹จ
  • {{ form.as_p }} : ํผ์˜ ๊ฐ ํ•„๋“œ๋ฅผ p ํƒœ๊ทธ ์•ˆ์—์„œ ๋ ˆ์ด๋ธ”๊ณผ ํ…์ŠคํŠธ๋กœ ๋ฐฐ์น˜ํ•œ๋‹ค. ์ž ์—ฌ๊ธฐ์„œ form์€??? ๋ฐ‘์—์„œ forms.py ์ž‘์„ฑํ• ๊ฑฐ์ž„. ใ„ฑใ„ท!
    ๐Ÿ“ƒ detail.html (๋ฆฌ๋ทฐ ์ƒ์„ธ)
<body>
    <h1>{{review.title}}</h1>
    <p>๊ฒŒ์‹œ๋ฌผ ๋ฒˆํ˜ธ : {{review.pk}}</p>
    <p>๊ธ€์“ด์ด : {{review.author}}</p>
    <p>{{review.body}}</p>
</body>

๐Ÿ“ƒ reviews.html (๋ฆฌ๋ทฐ ๋ชจ์•„๋ณด๊ธฐ)

<body>
    {%for obj in reviews.all%}
    <h1>์ œ๋ชฉ : {{obj.title}}</h1>
    <p>๋‚ด์šฉ :{{obj.body}}</p>
    <p>์˜ฌ๋ฆฐ ๋‚ ์งœ: {{boj.date}}</p>
    {%endfor%}
</body>
  • for๋ฌธ์„ ์จ์„œ Review ๊ฐ์ฒด ์ „์ฒด๋ฅผ ์ˆœํšŒํ•œ๋‹ค.
    ๐Ÿ“ƒ update.html (๋ฆฌ๋ทฐ ์ˆ˜์ •)
<body>
    <h1>์ˆ˜์ •ํ•˜๊ธฐ</h1>
    <form method="post" action="">
        {% csrf_token%}
        {{form.as_p}}
        <input type="submit" value="๋“ฑ๋กํ•˜๊ธฐ"/>
    </form>
</body>

html ํŒŒ์ผ๋“ค์„ ๋งŒ๋“ ๋‹ค.

โž• default time

์•„ ์ฐธ๊ณ ๋กœ default ์‹œ๊ฐ„์€ ๋ฏธ๊ตญ ์‹œ๊ฐ„์œผ๋กœ ๋งž์ถฐ์ ธ ์žˆ๋Š”๋ฐ ํ•œ๊ตญ์‹œ๊ฐ„์œผ๋กœ ๋ฐ”๊พธ๊ณ  ์‹ถ๋‹ค! ํ•˜๋ฉด
๐Ÿ“ƒ settings.py

TIME_ZONE = 'Asia/Seoul'

์ด๋ ‡๊ฒŒ ๋ฐ”๊พธ๋ฉด ๋œ๋‹ค.

๐Ÿšฉ forms.py

html ์ฝ”๋“œ๋กœ form์„ ๋งŒ๋“ค์ง€ ์•Š๊ณ  django๋ฅผ ํ†ตํ•ด์„œ ํผ์„ ์ƒ์„ฑํ•  ๊ฒƒ์ด๋‹ค!
์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ๋ฅผ ์ง์ ‘ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค!
app ํด๋” ์•ˆ์— forms.py๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.
๐Ÿ“ƒ forms.py

from django import forms
from .models import Review

class ReviewForm(forms.ModelForm):
    class Meta:
        model = Review
        fields = ['title', 'author','content']
  • from django import forms : django๊ฐ€ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ œ๊ณตํ•˜๋Š” forms ๋ชจ๋ธ์„ import
  • from .models import Review : Review ๋ชจ๋ธ์„ import
  • forms.ModelForm : ๋ชจ๋ธ๊ณผ ํ•„๋“œ๋ฅผ ์ง€์ •ํ•˜๋ฉด ๋ชจ๋ธํผ์ด ์ž๋™์œผ๋กœ ํผ ํ•„๋“œ๋ฅผ ์ƒ์„ฑ

๐Ÿšฉ Main / Create View

html์„ ๋ณด์—ฌ์ฃผ๊ธฐ ์œ„ํ•ด views.py๋ฅผ ์ž‘์„ฑํ•˜๊ณ  URL์—ฐ๊ฒฐ์„ ํ•ด์ค€๋‹ค.
๐Ÿ“ƒ views.py

from django.shortcuts import render, redirect # import redirect
from .models import Review
from .forms import ReviewForm # forms.py์—์„œ ReviewForm ๊ฐ€์ ธ์˜ค๊ธฐ

# Create your views here.
def main(request):
    reviews = Review.objects
    return render(request,'reviews.html',{'reviews':reviews})

def create(request):
    if request.method == 'POST': # method๊ฐ€ post์ผ๋•Œ
        form = ReviewForm(request.POST) # form ์— ReviewForm ํ• ๋‹น
        if form.is_valid(): # form ์œ ํšจ์„ฑ ๊ฒ€์ฆ
            form.save() # ์ €์žฅ
            return redirect('main') # ๋‹ค์‹œ main์œผ๋กœ
    else:
        form = ReviewForm() # ๋นˆ form ์—ด๊ธฐ
    return render(request, 'create.html',{'form' :form})
  • create์˜ if : ์‚ฌ์šฉ์ž๊ฐ€ POST๋ฐฉ์‹์œผ๋กœ request์š”์ฒญ์„ ๋ณด๋‚ด๋ฉด ์œ ํšจ์„ฑ ๊ฒ€์ฆ ๋’ค ๋‚ด์šฉ ์ €์žฅํ•˜๊ณ  main์œผ๋กœ ๋Œ์•„๊ฐ„๋‹ค.
  • create์˜ else : post ๋ฐฉ์‹์ด ์•„๋‹ˆ๋ฉด (์ƒˆ ๋ฆฌ๋ทฐ์“ฐ๊ธฐ ํŽ˜์ด์ง€๋ฅผ ์ฒ˜์Œ ์—ด์—ˆ์œผ๋ฉด) ReviewForm์„ ์—ด์–ด์ค€๋‹ค.
  • render(request, template, context) : review ๋ชจ๋ธ์„ ๋ถˆ๋Ÿฌ์˜ค๊ณ  renderํ•  ๋•Œ review ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ํ•จ๊ป˜ ๋ณด๋‚ด์ค€๋‹ค. template์—์„œ ํ˜ธ์ถœํ•  ๋‚ด์šฉ๋“ค์„ context ๋ถ€๋ถ„์— dictionary ํ˜•ํƒœ๋กœ ์ ์–ด์ค€๋‹ค.

๐Ÿ“ƒ urls.py

from django.contrib import admin
from django.urls import path
import review.views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('/',review.views.main,name="main"),
    path('create/',review.views.create,name='create')
]

๐Ÿšฉ Detail View

๐Ÿ“ƒ views.py

from django.shortcuts import render, redirect, get_object_or_404 
...
def detail(request, pk):
    review = get_object_or_404(Review,pk=pk)
    return render(request,'detail.html',{'review':review})
  • get_object_or_404 : ํ•ด๋‹น ๊ฐ์ฒด๊ฐ€ ์žˆ์œผ๋ฉด ๊ฐ€์ ธ์˜ค๊ณ  ์—†์œผ๋ฉด 404 error, pk๋กœ pk ์‚ฌ์šฉ
  • django์—์„œ๋Š” modelํ†ตํ•ด DB ์ƒ์„ฑ ์‹œ PK(๊ธฐ๋ณธํ‚ค)๋ฅผ ์ž๋™์œผ๋กœ ์ƒ์„ฑํ•ด์ค€๋‹ค!

๐Ÿ“ƒ urls.py

from django.contrib import admin
from django.urls import path
import review.views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('',review.views.main,name="main"),
    path('create/',review.views.create,name='create'),
    path('detail/<int:pk>/',review.views.detail,name='detail')
]

๐Ÿšฉ Update View

๐Ÿ“ƒ views.py

def update(request,pk):
    review = get_object_or_404(Review, pk=pk)
    if request.method == 'POST':
        form = ReviewForm(request.POST,instance=review) # review ๊ฐ์ฒด ๊ฐ€์ ธ์˜ค๊ธฐ
        if form.is_valid():
            form.save()
            return redirect('main')
    else:
        form = ReviewForm(instance=review) # review ๊ฐ์ฒด ๊ฐ€์ ธ์™€์„œ form์„ ์ƒ์„ฑ
    return render(request,'update.html',{'form':form})
  • review์— ์ด๋ฏธ ์ €์žฅ๋œ ๋‚ด์šฉ์„ ๋ถˆ๋Ÿฌ์˜จ๋‹ค.

๐Ÿ“ƒ urls.py

urlpatterns = [
    path('admin/', admin.site.urls),
    path('',review.views.main,name="main"),
    path('create/',review.views.create,name='create'),
    path('detail/<int:pk>/',review.views.detail,name='detail'),
    path('update/<int:pk>/',review.views.update,name='update')
]

๐Ÿšฉ Delete View

๐Ÿ“ƒ views.py

def delete(request,pk):
    review = Review.objects.get(pk=pk)
    review.delete() 
    return redirect('main')

๐Ÿ“ƒ urls.py

path('delete/<int:pk>/',review.views.delete,name='delete')

profile
๐Ÿš› ๋ธ”๋กœ๊ทธ ์ด์‚ฌํ•ฉ๋‹ˆ๋‹ค https://newwisdom.tistory.com/

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

comment-user-thumbnail
2021๋…„ 7์›” 27์ผ

์•ˆ๋…•ํ•˜์„ธ์š”~ ์ž˜๋ดค๋Š”๋ฐ ์งˆ๋ฌธ์ด ์žˆ์–ด์„œ ๊ธ€ ๋‚จ๊น๋‹ˆ๋‹ค
views.py์—์„œ render(request, template, context)์—ฌ๊ธฐ์„œ request๋Š” ์–ด๋–ค ์—ญํ• ์„ ํ•˜๋Š”๊ฑธ๊นŒ์š”?

๋‹ต๊ธ€ ๋‹ฌ๊ธฐ