티스토리 뷰

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>
댓글