장고 bootstrap templates 활용해서 아이템 이쁘게 정돈하기

1. modulus (%) 연산 함수를 정의

vi templatetags/utils.py

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

from django.template.defaulttags import register


@register.filter

def mod( value, arg ):

  return value % arg

* templatetags 디렉토리 안에 __init__.py 파일 생성


2. 기본 html 수정

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

{% block content %}

{% load utils %} 


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

  {% for d in contacts %}

    {% ifequal forloop.counter|mod:3 0 %}

    <div class="row">

    {% endifequal %}

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

        <div class="panel panel-success">

          <div class="panel-heading"> {{forloop.counter}} {{d.pname}}</div>

          <div class="panel-body"><img src="{{d.imgurl}}" class="img-responsive" style="width:100%" alt="Image"></div>

          <div class="panel-footer">{{d.brand}}</div>

        </div>

      </div>

    {% ifequal forloop.counter|mod:3 0 %}

    </div>

    {% endifequal %}

  {% endfor %}

</div><br>


{% endblock %}


3. 기본 html에서 동작 확인


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]

+ Recent posts