์ด์ ๋ ์ฐ๋ฆฌ๊ฐ ์์ฑํ๊ณ , ์กฐํํ๋ ๊ธ์ ์์ ํ๋ ๊ธฐ๋ฅ์ ์ถ๊ฐํด๋ณด๊ฒ ์ต๋๋ค. ์ ๋ฒ์ ํ๋ Create์ ๋น๊ต๋ฅผ ํด๋ณด์๋ฉด,
Create ๊ธฐ๋ฅ
<-> Update ๊ธฐ๋ฅ
new
: new.html์ ๋ณด์ฌ์ค <->edit
: edit.html์ ๋ณด์ฌ์ค
create
: ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ ์ฅ <-> update
: ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ ์ฅ
๋จ, update์์๋ ์์ ํ ๋ฐ์ดํฐ์ id๊ฐ์ ๋ฐ์์์ผ ํ๋ค๋ ์ฐจ์ด์ ์ด ์์ต๋๋ค.
def edit(request, id):
edit_blog = Blog.objects.get(id=id) #pk=id์ ๋์ผ
return render(request, 'main/edit.html', {'blog':edit_blog})
detailํจ์์ฒ๋ผ(Read) id๊ฐ์ ์ธ์๋ก ๋ฐ์์์ผ ํฉ๋๋ค. ๊ทธ๋ฆฌ๊ณ id=id
๋ถ๋ถ์ pk=id
๋ ๊ฐ๋ฅํฉ๋๋ค. ์ฐ๋ฆฌ๊ฐ models.py์์ id
๋ฅผ primary key๋ก ์ง์ ํด์ฃผ์๊ธฐ ๋๋ฌธ์
๋๋ค. (settings.py์ DEFAULT_AUTO_FIELD๋ฅผ ์ถ๊ฐํ ๊ฒฝ์ฐ๋ ์๋์ผ๋ก primary key๋ก id๊ฐ์ ์ ํํฉ๋๋ค.)
from django.urls import path
from .views import *
app_name = "main"
urlpatterns = [
path('', showmain, name="showmain"),
path('first/', first, name="first"),
path('second/', second, name="second"),
path('<str:id>', detail, name="detail"),
path('new/', new, name="new"),
path('create/', create, name="create"),
path('posts/', posts, name="posts"),
path('edit/<str:id>', edit, name="edit"),
]
url์ ์์ฑํด์ค๋๋ค. detail๊ณผ ๋์ผํ๋ฏ๋ก ์ค๋ช ์ ์๋ตํ๊ฒ ์ต๋๋ค.
main/templates/main/detail.html์ ์์ ํ๊ธฐ ๋ฒํผ(๋งํฌ)๋ฅผ ๋ง๋ค์ด์ค๋๋ค.
<p>{{blog.body}}</p>
<a href="{% url 'main:edit' blog.id %}">์์ ํ๊ธฐ</a>
app_name์ ์ค์ ํด์ฃผ์์ผ๋ main:edit
์ผ๋ก ์์ฑํด์ฃผ์ด์ผ ํฉ๋๋ค. detail๊ณผ ๋์ผํ๊ฒ ๊ฒ์๊ธ์ id๊ฐ์ ๋๊ฒจ์ฃผ์ด์ผ๊ฒ ์ฃ .
main/templates/main/edit.html
{% extends 'base.html' %}
{% load static %}
{% block content %}
<div class="container">
<h1>๊ธ ์์ ํ๊ธฐ</h1>
<form action="" method="post">
{%csrf_token%}
<p>์ ๋ชฉ : <input type="text" name="title" value="{{blog.title}}"></p>
<p>์์ฑ์ : <input type="text" name="writer" value="{{blog.writer}}"></p>
<p>๋ด์ฉ : <textarea type="text" name="body" id="" cols="30" rows="10">{{blog.body}}</textarea></p>
<button type="submit">์ ์ถ</button>
</form>
</div>
{% endblock %}
new.html๊ณผ ์ ์ฌํ๊ธฐ ๋๋ฌธ์ ๋ณต์ฌํด์์ ์์ ํ๋ฉด ํธํฉ๋๋ค. <h1>
์ ๋ด์ฉ์ ์์ ํ๊ธฐ๋ก ๋ฐ๊พธ์ด์ค๋๋ค. ๊ทธ๋ฆฌ๊ณ ์์ ํ ๋ ๊ธฐ์กด ๊ธ์ ๋ด์ฉ์ ํ
ํ๋ฆฟ ๋ณ์๋ฅผ ์ด์ฉํด ๋ถ๋ฌ์ค๋๋ก ์ค์ ํด์ค๋๋ค. <input>
์ ๊ฒฝ์ฐ value
๋ก ์ค์ ํด์ฃผ๊ณ , <textarea>
์ ๊ฒฝ์ฐ ํ๊ทธ ์ฌ์ด์ ์ ์ด์ฃผ์ด์ผ ํฉ๋๋ค.
def update(request, id):
update_blog = Blog.objects.get(id=id)
update_blog.title = request.POST['title']
update_blog.writer = request.POST['writer']
update_blog.pub_date = timezone.now()
update_blog.body = request.POST['body']
update_blog.save()
return redirect('main:detail', update_blog.id)
detail ํจ์์ ๋์ผํฉ๋๋ค.
from django.urls import path
from .views import *
app_name = "main"
urlpatterns = [
path('', showmain, name="showmain"),
path('first/', first, name="first"),
path('second/', second, name="second"),
path('<str:id>', detail, name="detail"),
path('new/', new, name="new"),
path('create/', create, name="create"),
path('posts/', posts, name="posts"),
path('edit/<str:id>', edit, name="edit"),
path('update/<str:id>', update, name="update"),
]
๋ง์ฐฌ๊ฐ์ง๋ก update ํจ์๋ url์ ์ฐ๊ฒฐํด์ค๋๋ค. id๊ฐ์ ๋๊ฒจ์ฃผ์ด์ผ ํด๋น ๊ฒ์๊ธ์ ๋ํ ๋ณ๊ฒฝ ์ฌํญ์ ์ ์ฅํ ์ ์๊ฒ ์ฃ .
{% extends 'base.html' %}
{% load static %}
{% block content %}
<div class="container">
<h1>๊ธ ์์ ํ๊ธฐ</h1>
<form action="{%url 'main:update' blog.id %}" method="post">
{%csrf_token%}
<p>์ ๋ชฉ : <input type="text" name="title" value="{{blog.title}}"></p>
<p>์์ฑ์ : <input type="text" name="writer" value="{{blog.writer}}"></p>
<p>๋ด์ฉ : <textarea type="text" name="body" id="" cols="30" rows="10">{{blog.body}}</textarea></p>
<button type="submit">์ ์ถ</button>
</form>
</div>
{% endblock %}
form์ action์ url์ ์ฐ๊ฒฐํด์ค๋๋ค. app_name์ ์ค์ ํด์ฃผ์์ผ๋ ์ฑ์ด๋ฆ:url์ด๋ฆ
ํํ๋ก ์ ์ด์ผํ๊ณ id๊ฐ๋ ๋๊ฒจ์ฃผ์ด์ผํฉ๋๋ค.
์ญ์ ๋ ๋งค์ฐ ๊ฐ๋จํฉ๋๋ค.
def delete(request, id):
delete_blog = Blog.objects.get(id=id)
delete_blog.delete()
return redirect('main:posts')
๋ง์ฐฌ๊ฐ์ง๋ก id๊ฐ์ ์ธ์๋ก ๋ฐ์์ค๊ณ ๊ทธ id์ ํด๋ฌํ๋ ๊ฒ์๊ธ์ ์ฐพ์ต๋๋ค. ๊ทธ๋ฆฌ๊ณ ์ญ์ ํด์ฃผ๋ฉด ๋ผ์. ์ญ์ ํ์๋ ๊ธ๋ชฉ๋ก ํ๋ฉด์ผ๋ก redirectํด์ค๋๋ค.
from django.urls import path
from .views import *
app_name = "main"
urlpatterns = [
path('', showmain, name="showmain"),
path('first/', first, name="first"),
path('second/', second, name="second"),
path('<str:id>', detail, name="detail"),
path('new/', new, name="new"),
path('create/', create, name="create"),
path('posts/', posts, name="posts"),
path('edit/<str:id>', edit, name="edit"),
path('update/<str:id>', update, name="update"),
path('delete/<str:id>', delete, name="delete"),
]
main/templates/main/detail.html
{% extends 'base.html' %}
{% load static %}
{% block content %}
<h1>{{blog.title}}</h1>
์์ฑ์ : {{blog.writer}}
๋ ์ง : {{blog.pub_date}}
<hr>
{% if blog.image %}
<p><img src="{{ blog.image.url }}" alt="์ฌ์ง"></p>
{% endif %}
<p>{{blog.body}}</p>
<a href="{% url 'main:edit' blog.id %}">์์ ํ๊ธฐ</a>
<a href="{% url 'main:delete' blog.id %}">์ญ์ ํ๊ธฐ</a>
{% endblock %}
update, delete๊น์ง ๋ชจ๋ ์๋ฃ!