티스토리 뷰
django project/블로그
[django 블로그] Post 게시물 목록 (views.py, @login_required, post_list.html, urls.py, 블로그 css, static)
지휘리릭 2020. 2. 2. 14:48● 게시물 목록
post/views.py
- 게시물 목록 관련 로직은 Post 객체를 모두 받아온 것을 html로 보내 나타낸다.
- 최신순으로 정렬한 게시물들을 posts에 저장하고 'post_list.html'와 같이 렌더링한다.
def post_list(request):
posts = Post.objects.order_by('-created_at')
return render(request, 'post_list.html', context = {'posts':posts})
templates/post_list.html
- 역시 html은 어렵다. 좀 더 블로그스럽게 css를 변경했다. 이번에는 테이블에서 글 목록을 정렬하는 방식아니라 텍스트 형식으로 나타낸다.
- a 태그를 사용하여 post-title 이나 post-subtitle을 클릭했을 때 해당 게시물의 상세보기 페이지로 이동하도록 한다.
- 제목과 내용을 표시하는데 truncatechars 를 사용하여 지정된 문자 수보다 긴 문자열은 잘라내서 "...." 으로 나타낸다.
- 작성자와 작성 시간을 아랫줄에 나타낸다.
{% extends 'base_.html' %}
{% block content %}
<div class = "container">
{% for post in posts %}
<div class = "col-lg-8 col-md-10 mx-auto">
<div class "post-preview">
<a href = "{% url 'post_detail' post.id %}">
<h2 class = "post-title">
{{post.title}}
</h2>
<h3 class = "post-subtitle">
{{post.content | truncatechars:10}}
</h3>
</a>
<p class = "post-meta">Posted by
<a href = "#">{{post.user}}</a>
{{post.created_at}}</p>
</div>
</div>
{% endfor %}
<div class = "row">
<div class = "col-lg-8 col-md-10 mx-auto">
<a href = "{% url 'post_write' %}" class = "btn btn-primary float-right">글쓰기</a>
</div>
</div>
</div>
{% endblock %}
실행 결과
'django project > 블로그' 카테고리의 다른 글
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 테이블출력안됨
- pythonanywhere배포방법
- 데이터베이스 추천
- pythonanywhere배포
- Django
- iOS 화면 안나옴
- 장고 게시판
- 장고 태그달기
- cleaned_data
- iOS 데이터베이스
- 웹 배포
- django 태그
- python 웹 배포
- django clean
- django 로그인접근
- ModelForm Form 차이
- Realtime Database
- django tag
- iOS 검은 화면
- django 게시판
- iOS UITableView 출력안됨
- UITableViewController Not Working
- CellForRowAt 호출안됨
- django 개발일지
- Firebase 데이터베이스 추천
- 알파벳 카운팅
- CellForRowAt Not Called
- django pythoneverywhere
- 실시간 데이터베이스
- 까만 화면
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
글 보관함