1. Paginator 함수로 아이템 분할

vi views.py

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

from __future__ import unicode_literals


from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger

from django.shortcuts import render

from django.views import View

from .models import *


# Create your views here.

class basic(View):

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

    #data = Product.objects.all()

    catid = '000006'

    if 'catid' in kwargs:

      catid = kwargs['catid']

    data = Product.objects.filter(cat1id=catid)


    # item number per page

    num_page = 10

    paginator = Paginator(data, num_page)

    page = request.GET.get('page')

    try:

      contacts = paginator.page(page)

    except PageNotAnInteger:

      # If page is not an integer, deliver first page.

      contacts = paginator.page(1)

    except EmptyPage:

      # If page is out of range (e.g. 9999), deliver last page of results.

      contacts = paginator.page(paginator.num_pages)


    return render(request, 'brand/basic.html', context={'catid':catid,'contacts':contacts})


2. 기본 html 에 pagination 생성

{% extends "brand/basic_frame.html" %}

{% block content %}


<div class="pagination">

  <span class="step-links">

    <ul class="pagination">

    {% if contacts.has_previous %}

      <li><a href="?page={{ contacts.previous_page_number }}"> prev </a></li>

    {% endif %}

      <li><a href="#"> Page {{ contacts.number }} of {{ contacts.paginator.num_pages }}. </a></li>

    {% if contacts.has_next %}

      <li><a href="?page={{ contacts.next_page_number }}"> next </a></li>

    {% endif %}

  </ul>

  </span>

</div>


<div class="container-fluid bg-3 text-center">

  <div class="row">

    {% for d in contacts %}

    <div class="col-sm-4">

      <div class="media">

        <div class="media-left media-middle">

          <img src="{{d.imgurl}}" class="media-object" width="100" height="100">

        </div>

        <div class="media-body">

          <h4> {{d.pname}} </h4>

          <p> {{d.brand}} </p>

        </div>

      </div>

    </div>

    {% endfor %}

  </div>

</div>


{% endblock %}


3. pagination 동작 확인




- Reference

https://www.w3schools.com/bootstrap/bootstrap_pagination.asp

1. 기본 프레임 html에 드롭다운 생성

cd demo ($app_name)

vi templates/demo/basic_frame.html

<!DOCTYPE html>

<html>

  <head>

    <title>Shop Demo</title>


    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

  </head>

  <body>

    <nav class="navbar navbar-inverse" role="navigation">

    <div class="container">

      <div class="navbar-header">

        <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false">

          <span class="sr-only">Toggle navigation</span>

          <span class="icon-bar"></span>

          <span class="icon-bar"></span>

          <span class="icon-bar"></span>

        </button>

        <a class="navbar-brand" href="#">Brand Demo</a>

      </div>

    </div>

    </nav>


    <div class="container">

      <div class="dropdown">

        <button class="btn btn-default dropdown-toggle" type="button" id="menu1" data-toggle="dropdown">Category

          <span class="caret"></span></button>

        <ul class="dropdown-menu" role="menu" aria-labelledby="menu1">

          <li role="presentation"><a role="menuitem" tabindex="-1" href="/basic/000004">문방구</a></li>

          <li role="presentation"><a role="menuitem" tabindex="-1" href="/basic/000006">패션의류</a></li>

          <li role="presentation"><a role="menuitem" tabindex="-1" href="/basic/000010">책/잡지</a></li>

          <li role="presentation"><a role="menuitem" tabindex="-1" href="/basic/000031">CD</a></li>

          <li role="presentation"><a role="menuitem" tabindex="-1" href="/basic/000041">키친용품</a></li>

          <li role="presentation"><a role="menuitem" tabindex="-1" href="/basic/000049">인테리어/가구</a></li>

          <li role="presentation"><a role="menuitem" tabindex="-1" href="/basic/000082">DIY</a></li>

          <li role="presentation"><a role="menuitem" tabindex="-1" href="/basic/000146">가전</a></li>

          <li role="presentation"><a role="menuitem" tabindex="-1" href="/basic/000162">컴퓨터</a></li>

          <li role="presentation"><a role="menuitem" tabindex="-1" href="/basic/000174">일상용품/생활용품</a></li>

        </ul>

      </div>

    </div>


    <div class="container">

      <div class="row">

        {% block content %}{% endblock %}

      </div>

    </div>

  </body>

</html>


2. 기본 html 작성

vi templates/demo/basic.html

{% extends "brand/basic_frame.html" %}

{% block content %}


<div class="container-fluid bg-3 text-center">

  <div class="row">

    {% for d in data%}

    <div class="col-sm-4">

      <div class="media">

        <div class="media-left media-middle">

          <img src="{{d.imgurl}}" class="media-object" width="100" height="100">

        </div>

        <div class="media-body">

          <h4> {{d.pname}} </h4>

          <p> {{d.brand}} </p>

        </div>

      </div>

    </div>

    {% endfor %}

  </div>

</div>


{% endblock %}


3. 드롭다운에서 얻는 카테고리 id (href="/basic/000004") url 주소로 파싱

vi urls.py

from django.conf.urls import url

from . import views


urlpatterns = [

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

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

]


4. url 주소에서 얻은 카테고리 id의 데이터 반영

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 basic(View):

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

    #data = Product.objects.all()

    catid = '000006'

    if 'catid' in kwargs:

      catid = kwargs['catid']

    data = Product.objects.filter(cat1id=catid)


    return render(request, 'brand/basic.html', context={'catid':catid,'data':data})


5. 드롭다운 동작 확인

https://~:8080/basic





- Reference

https://www.w3schools.com/bootstrap/bootstrap_dropdowns.asp

1. html 에서 css 정의하여 사용하기

vi templates/demo/base.html

<!doctype html>

<html lang="en">

  <head>

    <title>MyDjangoApp</title>


    <meta charset="utf-8">

    <meta name="viewport" content="width=device-width, initial-scale=1">


    <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

  </head>

  <body class="override">

    <nav class="navbar navbar-inverse" role="navigation">

    <div class="container">

      <div class="navbar-header">

        <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false">

          <span class="sr-only">Toggle navigation</span>

          <span class="icon-bar"></span>

          <span class="icon-bar"></span>

          <span class="icon-bar"></span>

        </button>

        <a class="navbar-brand" href="#">Django Pagination</a>

      </div>

    </div>

    </nav>


    <div class="container">

      <div class="row">

        {% block content %}{% endblock %}

      </div>

    </div>

  </body>

</html>


vi templates/demo/home.html

{% extends "demo/base.html" %}


{% block content %}

<div class="container products-view-user">

  {% csrf_token %}

  <form class = "post-list">

    <input type = "hidden" value = "" />

  </form>

  <article class="navbar-form navbar-left p-0 m-0 ml-b">

  <div class="form-group">

    <label>Per Page: </label>

    <select class="form-control post_max">

      <option value="1">1</option>

      <option value="2">2</option>

      <option value="3">3</option>

    </select>

    <label>Search Keyword: </label>

    <input type="text" class="form-control post_search_text" placeholder="Enter a keyword">

  </div>

  <input type = "submit" value = "Filter" class = "btn btn-primary post_search_submit" />

  </article>


  <a href="user-products-add.php" class="btn btn-success pull-right">Add New</a>


  <br class = "clear" />


  <div class = "wave-box-wrapper">

    <div class = "wave-box"></div>

    <table class = "table table-striped table-post-list no-margin">

      <thead>

        <tr>

          <th>Image</th>

          <th>Id</th>

          <th>Name</th>

          <th>Brand</th>

          <th>cat1id</th>

          <th>cat2id</th>

          <th>cat3id</th>

        </tr>

      </thead>

      <tbody class = "pagination-container">

        {% for d in data %}

        <tr>

          <td> <img src="{{d.imgurl}}" width="100" height="100"> </td>

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

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

          <td> {{d.brand}} </td>

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

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

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

        </tr>

        {% endfor %}

      </tbody>

    </table>


    <div class = "pagination-nav"></div>

  </div>

</div>

{% endblock %}


2. 기본 홈페이지에서 확인

http://~:8080/home




- Reference -

Python – Django AJAX Pagination with Search and Sort [Tutorial]

1. 장고 뷰에서 데이터 베이스 paginator로 설정 

cd demo

vi views.py

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

from __future__ import unicode_literals


from django.shortcuts import render

from django.views import View

from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger


from .models import *


# Create your views here.

class catetc_base(View):

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

    products = Product.objects.all()

    # 한 페이지에 보여줄 아이템 개수

    page_item_num = 5

    paginator = Paginator(products, page_item_num)

    page = request.GET.get('page')

    items = None

    try:

      items = paginator.page(page)

    except PageNotAnInteger:

      items = paginator.page(1)

    except EmptyPage:

      items = paginator.page(paginator.num_pages)


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


2. html 에서 페이지 표시 구현

vi templates/demo/demo_base.html

<!doctype html>

<html lang="ko">

<html>

  <head>

    <meta charset="utf-8">

    <title>Paginator Demo</title>

  </head>


  <body>

    <a>Products</a> <br>

    <table>

      <thead>

        <tr>

          <th> pid    </th>

          <th> cat1id </th>

          <th> cat2id </th>

          <th> cat3id </th>

          <th> pname  </th>

          </tr>

      </thead>

      <tbody>

        {% for w in items %}

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


    <div class="pagination">

    <span class="step-links">

      {% if items.has_previous %}

        <a href="?page={{ items.previous_page_number }}{% for key, value in request.GET.items %}{% if key != 'page' %}&{{ key }}={{ value }}{% endif %}{% endfor %}">previous</a>

      {% endif %}

      <span class="current">

        Page {{ items.number }} of {{ items.paginator.num_pages }}.

      </span>

      {% if items.has_next %}

        <a href="?page={{ items.next_page_number }}{% for key, value in request.GET.items %}{% if key != 'page' %}&{{ key }}={{ value }}{% endif %}{% endfor %}"> next </a> &nbsp

      {% endif %}

    </span>

    </div>

  </body>

</html>


1. 템플릿태그 폴더에 사용할 함수 생성

cd demo

mkdir templatetags

vi templatetags/util.py

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

from django.template.defaulttags import register


@register.filter

def div( value, arg ):

  '''

  Divides the value; argument is the divisor.

  Returns empty string on any error.

  '''

  try:

    value = int( value )

    arg = int( arg )

    if arg: return value / arg

  except: pass

  return ''


2. html 에 템플릿태그 함수 로드 후 사용

vi templates/demo/demo_base.html

<!doctype html>

{% load util %}


<html lang="ko">

<html>

  <head>

    <meta charset="utf-8">

    <title>Templatetags Demo</title>

  </head>

  <body>

    10 / 2 = {{ 10|div:2 }}

   </body>

</html>

* html templatetags format : $value | ${func_name} : $arg


3. 마이그레이션 하기  (make migration)

cd ..

python manage.py makemigrations demo

python manage.py migrate


4. 기본 홈페이지에서 결과 확인

* 서버를 리셋해줘야함

10 / 2 = 5


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. 데이터 베이스 프레임 생성 (models.py)

cd demo

vi models.py

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

from __future__ import unicode_literals


from django.db import models

from django.db.models import *


# Create your models here.

class Product(Model):

  # primary_key : only one value in this class

  pid = BigIntegerField(primary_key=True)

  # db_index : group value for searching faster

  cat1id = BigIntegerField(db_index=True)

  cat2id = BigIntegerField(db_index=True)

  cat3id = BigIntegerField(db_index=True)

  pname = TextField(default='-')


  status = TextField(default='none')


  def set_status(self, value):

    self.status = value

    self.save()


2. 새로운 모델의 migrations 생성 및 적용

cd ..

python manage.py makemigrations demo

python manage.py migrate demo


3. 데이터 베이스 생성

vi test.db

12345 001 002 003 Poorman T-shirts

12346 001 002 004 Poorman Boots

12347 001 003 003 Poorman Cap

12348 002 003 004 Poorman Watch

vi make_db.py

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

from __future__ import unicode_literals


import os, sys, django

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")

django.setup()

from catetc.models import *


class ModelObjects():

  def __init__(self, model):

    self.model = model


  def create(self, line):

    line = line.strip()

    words = line.split('\t')

    pid = words[0]

    cat1id = words[1]

    cat2id = words[2]

    cat3id = words[3]

    pname = words[4]

    self.model.objects.create(pid=pid, cat1id=cat1id, cat2id=cat2id, cat3id=cat3id, pname=pname)


  def delete(self):

    self.model.objects.all().delete()



def main():

  db = ModelObjects(Product)

  db.delete()

  for line in sys.stdin:

    line = line.strip()

    if not line:  continue


    db.create(line)


# main

if __name__ == '__main__':

  main()

cat test.db | python make_db.py


5. admin (관리자) 모드에서 볼 수 있게 admin 등록

vi demo/admin.py

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

from __future__ import unicode_literals


from django.contrib import admin

from .models import *


# Register your models here.

admin.site.register(Product)


6. admin 홈페이지에서 데이터 베이스 확인

http://~:8080/admin


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


0. Django 장고 프로젝트 경로 설정

project_path = './demo'

app_name = 'demo'


1. Django 장고 프로젝트 생성

django-admin startproject mysite $project_path


2. Django 장고 프로젝트 APP 생성

cd $project_path

python manage.py migrate

python manage.py startapp $app_name


vi mysite/settings.py

ALLOWED_HOSTS = [

'192.168.0.1',

]


INSTALLED_APPS = [

    'django.contrib.admin',

    'django.contrib.auth',

    'django.contrib.contenttypes',

    'django.contrib.sessions',

    'django.contrib.messages',

    'django.contrib.staticfiles',

    'demo',

]


TIME_ZONE = 'Asia/Seoul'

python manage.py makemigrations $app_name

python manage.py migrate $app_name


3. Django 장고 프로젝트 슈퍼 유저 생성

python manage.py createsuperuser


4. Django 장고 프로젝트 실행

python manage.py runserver 0:8000


- 결과

http://~:8080/


+ Recent posts