티스토리 뷰
urls.py
from django.contrib import admin
from django.urls import path, re_path
from dkea.views import *
urlpatterns = [
re_path(r'^admin/', admin.site.urls),
re_path(r'^$', MainView, name='main'),
re_path(r'^list/(?P<c_code>c\d+)/$', ProductListView, name='list'),
re_path(r'^detail/(?P<p_id>\d+)/$', ProductDetailView, name='detail'),
]
views.py
① MainView
def MainView(request):
try:
cursor = connection.cursor()
strSql = "SELECT DISTINCT(c_code), c_name"
strSql += " FROM category"
result = cursor.execute(strSql)
datas = cursor.fetchall()
connection.commit()
connection.close()
categories = []
for data in datas:
row = {'c_code': data[0],
'c_name': data[1],}
categories.append(row)
except:
connection.rollback()
print("Failed selecting in MainView")
render_page = "main.html"
return render(request, render_page, {"categories": categories,})
② ProductListView
def ProductListView(request, c_code):
try:
cursor = connection.cursor()
strSql = "SELECT c_name"
strSql += " FROM category"
strSql += " WHERE c_code = (%s)"
result = cursor.execute(strSql, (c_code,))
datas = cursor.fetchall()
c_name = datas[0][0]
strSql = "SELECT p.p_id, p.p_name, p.img_src, p.price"
strSql += " FROM product p"
strSql += " JOIN category c ON c.c_id = p.c_id"
strSql += " WHERE c.c_code = (%s)"
result = cursor.execute(strSql, (c_code,))
datas = cursor.fetchall()
connection.commit()
connection.close()
products = []
for data in datas:
row = {'p_id': data[0],
'p_name': data[1],
'img_src': data[2],
'price': data[3],}
products.append(row)
except:
connection.rollback()
print("Failed selecting in ProductListView")
render_page = "product_list.html"
return render(request, render_page, {'c_name': c_name, 'products': products,})
③ ProductDetailView
def ProductDetailView(request, p_id):
try:
cursor = connection.cursor()
strSql = "SELECT c.c_name, c.i_name, p.p_id, p.p_name, p.img_src, p.price, p.link"
strSql += " FROM product p"
strSql += " JOIN category c ON c.c_id = p.c_id"
strSql += " WHERE p.p_id = (%s)"
result = cursor.execute(strSql, (p_id,))
datas = cursor.fetchall()
connection.commit()
connection.close()
product = {'c_name': datas[0][0],
'i_name': datas[0][1],
'p_id': datas[0][2],
'p_name': datas[0][3],
'img_src': datas[0][4],
'price': datas[0][5],
'link': datas[0][6],}
except:
connection.rollback()
print("Failed selecting in ProductListView")
render_page = "product_detail.html"
return render(request, render_page, {'product': product})
Templates
① main.html
<body>
<h1>DKEA</h1>
<ul>
{% for one in categories %}
<button onclick="window.location.href='{% url 'list' one.c_code %}'">{{one.c_name}}</button>
{% endfor %}
</ul>
</body>
② product_list.html
<head>
<meta charset="UTF-8">
<title>DKEA</title>
<style>
.productBox {
display: inline-block;
}
.innerImg {
width: 280px;
height: 300px;
padding: 4px;
}
</style>
</head>
<body>
<h1>Product List</h1>
<h3>카테고리 >> {{c_name}} </h3>
{% for one in products %}
<div class="productBox">
<div class="productImg">
<a href="{% url 'detail' one.p_id %}">
<img src="{{one.img_src}}" alt="상품 이미지" class="innerImg">
</a>
</div>
<div class="productInfo">
<a href="{% url 'detail' one.p_id %}"><h5>{{one.p_name}}</h5></a>
<p>{{one.price}}원</p>
</div>
</div>
{% endfor %}
</body>
③ product_detail.html
<head>
<meta charset="UTF-8">
<title>DKEA</title>
<style>
.productBox {
text-align:center;
}
</style>
</head>
<body>
<h1>Product Detail</h1>
<h3>카테고리 >> {{product.c_name}} >> {{product.i_name}}</h3>
<div class="productBox">
<div class="ProductImg">
<a href="{{product.link}}">
<img src="{{product.img_src}}" alt="상품 이미지" class="innerImg">
</a>
</div>
<div class="productInfo">
<h5>{{product.p_name}}</h5>
<p>{{product.price}}원</p>
</div>
</div>
</body>
'Django' 카테고리의 다른 글
[Django App 2-3] settings.py에서 MySQL 서버 연결 정보 분리하기 (0) | 2020.01.19 |
---|---|
[Django Basic 04] Messages Framework (0) | 2020.01.16 |
[issue #002] django.db.utils.OperationalError: (2003, "Can't connect to MySQL server on 'localhost' (10061)") (0) | 2020.01.09 |
[issue #001] mysqlclient가 설치되지 않을 때 (0) | 2020.01.09 |
[Django App 2-3] Django에서 MySQL 활용하기 (2) | 2020.01.09 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- Django application
- java
- Django 해시태그
- query parameter
- python
- Django User
- Django 어플리케이션
- Django 로그아웃
- Django 댓글
- MySQL
- Django 검색
- Django Instagram
- Django 좋아요
- Django 회원가입
- Django 프로젝트 생성
- 북마크 어플리케이션
- Django 회원 정보 수정
- 서점 어플리케이션
- Django 비밀번호 수정
- 파이썬
- Django
- Django 컬렉션
- Django 북마크
- Django 팔로우
- Django 업로드
- Redis
- 장고
- Django 인스타그램
- Redis Cache
- 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 |
글 보관함