1. 데이터 베이스 전달

cd demo

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):

    data = Product.objects.all()

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


2. html 에서 데이터 베이스 표시

vi templates/demo/demo_base.html

<!doctype html>

<html lang="ko">

<html>

  <head>

    <meta charset="utf-8">

    <title>Category Demo</title>

  </head>


  <body>

    <table>

      <thead>

        <tr>

          <th> pid    </th>

          <th> cat1id </th>

          <th> cat2id </th>

          <th> cat3id </th>

          <th> pname  </th>

          </tr>

      </thead>

      <tbody>

        {% for w in data %}

        <tr>

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

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

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

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

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

        </tr>

        {% endfor %}

      </tbody>

    </table>

  </body>


</html>


3. 기본 홈페이지에서 데이터 확인

http://~:8080/demo


+ Recent posts