본문 바로가기
  • 소소한 개발자 이야기
Python & Django/Django Framework 실전

[view] 상품 목록 만들기

by Siwan_Min 2020. 7. 27.
728x90

Views.py 수정

product > views.py

1
2
3
4
5
6
7
from django.shortcuts import render
from django.views.generic import ListView, DetailView
# Create your views here.
class ProductList(ListView):
    model = Product
    template_name = 'product.html'
    context_object_name = 'product_list'

 

urls 연결 

 

fc_django > urls.py

 

path('product/', ProductList.as_view()), 지난 글에서 설정했던 코드에서 product의 path도

추가해주세요.

1
2
3
4
5
6
7
8
9
10
11
12
from django.contrib import admin
from django.urls import path
from fcuser.views import index, RegisterView, LoginView
 
urlpatterns = [
    path('admin/', admin.site.urls),
    path('', index),
    path('register/', RegisterView.as_view()), #클래스는 .as_view() 를 해주어야 함
    path('login/', LoginView.as_view()),
   path('product/', ProductList.as_view()),
]
 

 

앱 추가

fc_django > settings.py 로 가셔서 'django.contrib.humanize' 도 추가해 주세요 

humanaize는 django에 내장되어 있는 앱인데요, 

우리 엑셀을 사용할때 시간이나, 가격, 돈 이런거 필터 걸수 있잖아요! 그런 역할을 해주는 앱이라고 

생각하시면 됩니다. 앞으로 humanazie 라는 앱을 통해서 html에 필터를 걸어줄거에요 

fc_django > settings.py

 

product.html 생성

product > templeates에 product.html 을 생성해 주세요 

product.html 생성

조금 전에 

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
{% extends "base.html" %}
{% load humanize %}
{% block contents %}
<div class="row mt-5">
    <div class="col-12">
        <table class="table table-light">
            <thead class="thead-light">
                <tr>
                    <th space="col">#</th>
                    <th space="col">상품명</th>
                    <th space="col">가격</th>
                    <th space="col">등록날짜</th>
                </tr>
            </thead>
            <tbody class="text-dark">
                {% for product in product_list %}
                <tr>
                    <th space="row">{{ product.id }}</th>
                    <th>{{ product.name }}</th>
                    <th>{{ product.price|intcomma }} 원</th>
                    <th>{{ product.register_date|date:'Y-m-d H:i' }}</th>
                </tr>
                {% endfor %}
            </tbody>
        </table>
    </div>
</div>
{% for product in product_list %}
{% endfor %}
{% endblock %}
 

 

이렇게 하시고 나서 python manage.py runserver 하고나서

127.0.0.1:8000/product 로 들어가보시면 아래와 같이 리스트가 생긴것을 볼 수 있으실겁니다. 

product  list생성 

 

728x90

댓글