Django로 인스타그램 만들기 - 08. Post 상세 페이지
참고문서
Post 상세 화면을 위한 view, template, url구현
post/views.py
def post_detail(request, post_pk):
post = get_object_or_404(Post, pk=post_pk)
comment_form = CommentForm()
context = {
'post': post,
'comment_form': comment_form,
}
return render(request, 'post/post_detail.html', context)
templates/post/post_detail.html
{% load static %}
<!doctype html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="{% static 'css/bootstrap.min.css' %}">
<link rel="stylesheet" href="{% static 'css/style.css' %}">
<title>Instagram</title>
</head>
<body>
<div class="container">
{% if messages %}
{% for message in messages %}
<div class="alert-message alert
{% if message.tags == 'success' %}
alert-success
{% elif message.tags == 'error' %}
alert-danger
{% endif %}
">{{ message }}</div>
{% endfor %}
{% endif %}
<h1>Post Detail</h1>
<div class="panel panel-default post-item">
<img src="{{ post.photo.url }}" alt="" class="post-image">
<div class="panel-body">
{% if post.comments.exists %}
<ul class="comment-list">
{% for comment in post.comments.all %}
<li class="comment">
<span class="comment-author">{{ comment.author }}</span>
<span class="comment-content">{{ comment.content }}</span>
</li>
{% endfor %}
</ul>
<hr>
{% endif %}
<form action="{% url 'post:comment_create' post_pk=post.pk %}" method="POST" class="comment-form">
{% csrf_token %}
{{ comment_form.content }}
</form>
</div>
</div>
</div>
</body>
</html>
post/urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.post_list, name='post_list'),
url(r'^(?P<post_pk>\d+)/$', views.post_detail, name='post_detail'),
url(r'^(?P<post_pk>\d+)/comment/create/$', views.comment_create, name='comment_create'),
]
Post목록에서 Post상세화면으로 이동할 수 있는 링크 추가
templates/post/post_list.html
...
{% for post in posts %}
<div class="panel panel-default post-item">
<a href="{% url 'post:post_detail' post_pk=post.pk %}">
<img src="{{ post.photo.url }}" alt="" class="post-image">
</a>
...