티스토리 뷰
django project/쇼핑몰
[django 쇼핑몰] Product List 상품 목록 (views.py, decorator, ListView, templates, product_list.html, {%load humanize%})
지휘리릭 2020. 1. 24. 13:11● 상품 목록 (ListView ver.)
product/views.py(ListView)
- ListView는 조건에 맞는 여러 개의 객체를 보여준다. 보여주려는 객체(model)를 Product로 정의한다. 그러면 Product 객체의 정보를 QuerySet으로 받아와서 template_name 에 연결된 html로 넘겨준다.
class ProductList(ListView):
model = Product
template_name = 'product_list.html'
templates/product_list.html
- ProductList 에서 받아온 product 객체 정보는 object_list에 저장된다. 객체리스트에는 여러 개의 product 객체 정보가 담겨있고, 그것을 하나씩 가져와서 화면에 출력하면 된다.
{% extends 'base.html' %}
{% load humanize %}
{% block contents %}
<div class = "row mt-5">
<div class = "col-12">
<table class = "table table-dark">
<thead class = "thead thead-dark">
<td scope = "col">No.</td>
<td scope = "col">상품명</td>
<td scope = "col">가격</td>
<td scope = "col">등록 날짜</td>
</thead>
<tbody class = "text-light">
{% for product in object_list %}
<tr>
<td scope = "col">{{product.id}}</td>
<td>{{ product.name }}</td>
<td>{{ product.price | intcomma }}원</td>
<td>{{ product.registered_date | date:'Y-m-d H:i'}}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
{% endblock %}
- 세자리 단위로 콤마를 찍기 위해 {% load humanize %} 를 상단에 추가한다. 그런데 settings.py INSTALLED_APP 에 'django.contrib.humanize' 추가해야 아래와 같은 오류가 발생하지 않는다.
상품 목록 페이지
'django project > 쇼핑몰' 카테고리의 다른 글
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- Firebase 데이터베이스 추천
- 장고 태그달기
- cleaned_data
- django pythoneverywhere
- Django
- 데이터베이스 추천
- iOS 화면 안나옴
- django 태그
- django 로그인접근
- 까만 화면
- 알파벳 카운팅
- iOS UITableView 출력안됨
- ModelForm Form 차이
- python 웹 배포
- 장고 게시판
- UITableViewController Not Working
- pythonanywhere배포방법
- CellForRowAt Not Called
- django clean
- iOS 검은 화면
- Realtime Database
- pythonanywhere배포
- CellForRowAt 호출안됨
- 실시간 데이터베이스
- django tag
- iOS 데이터베이스
- django 개발일지
- 웹 배포
- django 게시판
- 테이블출력안됨
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함