Skip to content

Latest commit

ย 

History

History
174 lines (151 loc) ยท 6.17 KB

File metadata and controls

174 lines (151 loc) ยท 6.17 KB

Update

์ด์ œ๋Š” ์šฐ๋ฆฌ๊ฐ€ ์ž‘์„ฑํ•˜๊ณ , ์กฐํšŒํ–ˆ๋˜ ๊ธ€์„ ์ˆ˜์ •ํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ €๋ฒˆ์— ํ–ˆ๋˜ Create์™€ ๋น„๊ต๋ฅผ ํ•ด๋ณด์ž๋ฉด, Create ๊ธฐ๋Šฅ <-> Update ๊ธฐ๋Šฅ new: new.html์„ ๋ณด์—ฌ์คŒ <->edit: edit.html์„ ๋ณด์—ฌ์คŒ create: ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ €์žฅ <-> update: ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ €์žฅ

๋‹จ, update์—์„œ๋Š” ์ˆ˜์ •ํ•  ๋ฐ์ดํ„ฐ์˜ id๊ฐ’์„ ๋ฐ›์•„์™€์•ผ ํ•œ๋‹ค๋Š” ์ฐจ์ด์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

1. main/views.py์— edit ํ•จ์ˆ˜ ์ž‘์„ฑํ•˜๊ธฐ (View)

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๊ฐ’์„ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค.)

2. main/urls.py์— url ์ถ”๊ฐ€ํ•˜๊ธฐ (Url)

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๊ณผ ๋™์ผํ•˜๋ฏ€๋กœ ์„ค๋ช…์€ ์ƒ๋žตํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

3. detail.html ์ž‘์„ฑํ•˜๊ธฐ (Template)

main/templates/main/detail.html์— ์ˆ˜์ •ํ•˜๊ธฐ ๋ฒ„ํŠผ(๋งํฌ)๋ฅผ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.

<p>{{blog.body}}</p>
<a href="{% url 'main:edit' blog.id %}">์ˆ˜์ •ํ•˜๊ธฐ</a>

app_name์„ ์„ค์ •ํ•ด์ฃผ์—ˆ์œผ๋‹ˆ main:edit์œผ๋กœ ์ž‘์„ฑํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. detail๊ณผ ๋™์ผํ•˜๊ฒŒ ๊ฒŒ์‹œ๊ธ€์˜ id๊ฐ’์„ ๋„˜๊ฒจ์ฃผ์–ด์•ผ๊ฒ ์ฃ .

4. edit.html ์ƒ์„ฑํ•˜๊ธฐ (Template)

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>์˜ ๊ฒฝ์šฐ ํƒœ๊ทธ ์‚ฌ์ด์— ์ ์–ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

5. main/views.py์— update ํ•จ์ˆ˜ ์ถ”๊ฐ€ํ•˜๊ธฐ(View)

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 ํ•จ์ˆ˜์™€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค.

6. main/urls.py์— url์ถ”๊ฐ€ํ•˜๊ธฐ (Url)

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๊ฐ’์„ ๋„˜๊ฒจ์ฃผ์–ด์•ผ ํ•ด๋‹น ๊ฒŒ์‹œ๊ธ€์— ๋Œ€ํ•œ ๋ณ€๊ฒฝ ์‚ฌํ•ญ์„ ์ €์žฅํ•  ์ˆ˜ ์žˆ๊ฒ ์ฃ .

7. edit.html์— form action ์„ค์ •ํ•˜๊ธฐ (Template)

{% 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๊ฐ’๋„ ๋„˜๊ฒจ์ฃผ์–ด์•ผํ•ฉ๋‹ˆ๋‹ค.

Delete

์‚ญ์ œ๋Š” ๋งค์šฐ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค.

1. main/views.py์— delete ํ•จ์ˆ˜ ์ถ”๊ฐ€ํ•˜๊ธฐ (View)

def delete(request, id):
    delete_blog = Blog.objects.get(id=id)
    delete_blog.delete()
    return redirect('main:posts')

๋งˆ์ฐฌ๊ฐ€์ง€๋กœ id๊ฐ’์„ ์ธ์ž๋กœ ๋ฐ›์•„์˜ค๊ณ  ๊ทธ id์— ํ•ด๋‹ฌํ•˜๋Š” ๊ฒŒ์‹œ๊ธ€์„ ์ฐพ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์‚ญ์ œํ•ด์ฃผ๋ฉด ๋ผ์š”. ์‚ญ์ œํ›„์—๋Š” ๊ธ€๋ชฉ๋ก ํ™”๋ฉด์œผ๋กœ redirectํ•ด์ค๋‹ˆ๋‹ค.

2. main/urls.py์— url ์ถ”๊ฐ€ํ•˜๊ธฐ(Url)

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"),
]

3. detail.html์— ์‚ญ์ œ ๋ฒ„ํŠผ ๋งŒ๋“ค์–ด์ฃผ๊ธฐ (Template)

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๊นŒ์ง€ ๋ชจ๋‘ ์™„๋ฃŒ!