<menu 프레임에서 상품 ID를 클릭하면 content 프레임에서 상품 정보를 보여주는 예제>

* 기존 튜토리얼 app name의 demo를 catetc로 변경하여 진행


1. 웹 프레임 menu 페이지에 데이터 베이스 pid 연동

[데이터 베이스] http://poorman.tistory.com/384

vi views.py

# -*- coding: utf-8 -*-

from __future__ import unicode_literals


from django.shortcuts import render

from django.views import View


from .models import *


# Create your views here.

class catetc_base(View):

  def get(self, request, *args, **kwargs):

    return render(request, 'catetc/catetc_base.html', context={})


class catetc_title(View):

  def get(self, request, *args, **kwargs):

    return render(request, 'catetc/catetc_title.html', context={})


class catetc_menu(View):

  def get(self, request, *args, **kwargs):

    products = Product.objects.all()

    return render(request, 'catetc/catetc_menu.html', context={'products':products})


class catetc_content(View):

  def get(self, request, *args, **kwargs):

    return render(request, 'catetc/catetc_content.html', context={})


2. 웹 프레임 content 페이지에서 pid (상품id) 전달 받을 수 있도록 url 설정

 vi urls.py

from django.conf.urls import url

from . import views


urlpatterns = [

  url(r'^catetc/$',views.catetc_base.as_view(), name='catetc_base'),

  url(r'^catetc/title/$',views.catetc_title.as_view(), name='catetc_title'),

  url(r'^catetc/menu/$',views.catetc_menu.as_view(), name='catetc_menu'),

  url(r'^catetc/content/$',views.catetc_content.as_view(), name='catetc_content'),

  url(r'^catetc/content/(?P<pid>[-\w]+)/$',views.catetc_content.as_view(), name='catetc_content'),

]


3. menu 프레임에서 content 프레임으로 pid 전달하도록 설정

vi templates/catetc/catetc_menu.html

<!doctype html>

<html lang="ko">

<html>

  <head>

    <meta charset="utf-8">

    <title>Category Demo</title>

  </head>

  <body>

    <a>Menu Product Ids</a> <br>

    {% for p in products %}

      <a href="{% url 'catetc_content' pid=p.pid %}" target='content'> {{ p.pid }} </a> <br>

    {% endfor %}

  </body>

</html>


4. content 프레임으로 pid 가 전달된 경우에 상품 데이터 베이스를 읽어들임

# -*- coding: utf-8 -*-

from __future__ import unicode_literals


from django.shortcuts import render

from django.views import View


from .models import *


# Create your views here.

class catetc_base(View):

  def get(self, request, *args, **kwargs):

    return render(request, 'catetc/catetc_base.html', context={})


class catetc_title(View):

  def get(self, request, *args, **kwargs):

    return render(request, 'catetc/catetc_title.html', context={})


class catetc_menu(View):

  def get(self, request, *args, **kwargs):

    products = Product.objects.all()

    return render(request, 'catetc/catetc_menu.html', context={'products':products})


class catetc_content(View):

  def get(self, request, *args, **kwargs):

    if 'pid' in kwargs:

      pid = kwargs['pid']

      prod = Product.objects.get(pid=pid)

      return render(request, 'catetc/catetc_content.html', context={'prod':prod})

    else:

      return render(request, 'catetc/catetc_content.html', context={})


5. content 프레임에서 상품 정보 표시

vi templates/catetc/catetc_content.html

<!doctype html>

<html lang="ko">

<html>

  <head>

    <meta charset="utf-8">

    <title>Category Demo</title>

  </head>

  <body>

    <a>Product Info</a> <br>

    <table>

      <thead>

        <tr>

          <th> pid    </th>

          <th> cat1id </th>

          <th> cat2id </th>

          <th> cat3id </th>

          <th> pname  </th>

          </tr>

      </thead>

      <tbody>

        <tr>

          <td> {{ prod.pid }}    </td>

          <td> {{ prod.cat1id }} </td>

          <td> {{ prod.cat2id }} </td>

          <td> {{ prod.cat3id }} </td>

          <td> {{ prod.pname }}  </td>

        </tr>

      </tbody>

    </table>

    </body>

</html>


6. 기본 홈페이지에서 프레임 동작 확인

http://~:8080/catetc


+ Recent posts