์ด ์ชฝ ์ ๊ณต ํ๋ค๋ฏ... CRUD ํ ๋ฒ์ฏค ๋ค์ด๋ดค์๊ฑฐ์ ...
๐ฅ Create : ๋ฐ์ดํฐ ์์ฑ
๐ Read : ๋ฐ์ดํฐ ๋ถ๋ฌ์ค๊ธฐ
โ๏ธ Update : ๋ฐ์ดํฐ ์์ / ๊ฐฑ์
๐ค Delete : ๋ฐ์ดํฐ ์ญ์ ํ๊ธฐ
์ ๊ทธ๋ผ ์ ๋ฒ์ ์ด์ด์ ๋ณธ๊ฒฉ์ ์ผ๋ก CRUD ๊ธฐ๋ฅ์ ๊ตฌํํ๋ค!
์ด๊ฑฐ ๊ตฌ๊ธ๋งํ๋ค ๋ฐ๊ฒฌํ ์์ธ๋ฐ ์ฉ ๋ง์์ ๋๋คใ
์์ฃผ ๋ฑ์ฅํ ์์ ๐
๋๋ Review๋ฅผ ์ฐ๊ณ , ์ฝ๊ณ , ์์ ํ๊ณ , ์ญ์ ํ๋ ๊ธฐ๋ฅ์ ๊ตฌํํ ๊ฑฐ๋ค.
๐ create.html (๋ฆฌ๋ทฐ ์ฐ๊ธฐ)
<body>
<h1>๋ฆฌ๋ทฐ ์ฐ๊ธฐ</h1>
<form method="POST" action=''>
{%csrf_token%}
{{form.as_p}}
<input type = 'submit' value = '๋ฑ๋กํ๊ธฐ'/>
</form>
</body>
<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>
<body>
<h1>์์ ํ๊ธฐ</h1>
<form method="post" action="">
{% csrf_token%}
{{form.as_p}}
<input type="submit" value="๋ฑ๋กํ๊ธฐ"/>
</form>
</body>
html ํ์ผ๋ค์ ๋ง๋ ๋ค.
์ ์ฐธ๊ณ ๋ก default ์๊ฐ์ ๋ฏธ๊ตญ ์๊ฐ์ผ๋ก ๋ง์ถฐ์ ธ ์๋๋ฐ ํ๊ตญ์๊ฐ์ผ๋ก ๋ฐ๊พธ๊ณ ์ถ๋ค! ํ๋ฉด
๐ settings.py
TIME_ZONE = 'Asia/Seoul'
์ด๋ ๊ฒ ๋ฐ๊พธ๋ฉด ๋๋ค.
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']
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})
๐ 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')
]
๐ 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})
๐ 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')
]
๐ 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})
๐ 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')
]
๐ 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')
์๋ ํ์ธ์~ ์๋ดค๋๋ฐ ์ง๋ฌธ์ด ์์ด์ ๊ธ ๋จ๊น๋๋ค
views.py์์ render(request, template, context)์ฌ๊ธฐ์ request๋ ์ด๋ค ์ญํ ์ ํ๋๊ฑธ๊น์?