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


1. Django 프로젝트 urls 설정

vi mysite/urls.py

from django.conf.urls import url, include

from django.contrib import admin


urlpatterns = [

  url(r'^admin/', admin.site.urls),

  url(r'', include('demo.urls')),

]

cd demo

vi urls.py

from django.conf.urls import url

from . import views


urlpatterns = [

  url(r'^demo/$',views.demo_base.as_view(), name='demo_base'),

]


2. Django 프로젝트 views 설정

vi views.py

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

from __future__ import unicode_literals


from django.shortcuts import render

from django.views import View


# Create your views here.

class demo_base(View):

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

    return render(request, 'demo/demo_base.html', context={})


3. Django 프로젝트 html 생성

mkdir -p templates/demo

vi templates/demo/demo_base.html

<!doctype html>

<html lang="ko">

<html>

  <head>

    <meta charset="utf-8">

    <title>Django Demo</title>

  </head>


  <body>

    <a href="http://poorman.tistory.com">

      <img src="https://t1.daumcdn.net/cfile/tistory/2368C93A587F63331A"> &nbsp

      Poorman Tistory

    </a>

  </body>


</html>


- 결과

http://~:8080/demo


+ Recent posts