day19

怎么在自己的项目里加入 bootstrap jqucry

下载下来再拷贝到项目里(jqucry1.0支持IE 8,9的)

前端展示用户列表:

自定义分页(模块化)

把分页写成模块,以后用到分页的时候可以调用:

1、在Django中先生成一个app00,注册,配置url&views

from django.conf.urls import url
from django.contrib import admin
from app00 import views

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^user_list/',views.user_list),
]
配置url
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'app00',
]
注册app00
from django.db import models
class UserProfile(models.Model):
    user = models.OneToOneField(User) #alex
    name = models.CharField(max_length=64)
    school = models.ForeignKey('School')
配置models

2、分页代码

from django.shortcuts import render,redirect
import models
from django.core.paginator import Paginator,EmptyPage,PageNotAnInteger
from crm import forms
from  permissions import  check_permission
# Create your views here.

def dashboard(request):
    return render(request,'crm/dashboard.html')


@check_permission
def customers(request):
    customer_list = models.Customer.objects.all()
    paginator = Paginator(customer_list,3)
    page = request.GET.get('page')
    try:
        customer_objs = paginator.page(page)
    except PageNotAnInteger:
        customer_objs = paginator.page(1)
    except EmptyPage:
         customer_objs = paginator.page(paginator.num_pages)
    return render(request,'crm/customers.html',{'customer_list':customer_objs})

@check_permission
def customer_detail(request,customer_id):
    customer_obj = models.Customer.objects.get(id=customer_id)
    if request.method == "POST":#后台判断POST
        form = forms.CustomerModelForm(request.POST,instance=customer_obj)
        #print(request.POST)
        if form.is_valid():#修改之前先作验证
            form.save()
            print( 'url:',request.path)#获取url
            base_url = "/".join(request.path.split("/")[:-2])
            print( 'url:',base_url)
            return redirect(base_url)
        #else:
    else:
        form = forms.CustomerModelForm(instance=customer_obj)
    return render(request,'crm/customer_detail.html',{'customer_form':form})
配置views创建数据
{% extends 'base.html' %}
{% load custom_tags %}
{% block page-header %}
客户信息列表
{% endblock %}
{% block page-content %}
    <table class="table table-hover">
        <thead>
            <tr>
                <th>ID</th>
                <th>QQ</th>
                <th>姓名</th>
                <th>渠道</th>
                <th>咨询课程</th>
                <th>课程类型</th>
                <th>客户备注</th>
                <th>状态</th>
                <th>课程顾问</th>
                <th>日期</th>
            </tr>
        </thead>
        <tbody>
        {% for customer in customer_list %}
        <tr>
            <td><a href="{% url 'customer_detail' customer.id  %}">{{ customer.id }}</a></td>
            <td>{{ customer.qq}}</td>
            <td>{{ customer.name}}</td>
            <td>{{ customer.source}}</td>
            <td>{{ customer.course}}</td>
            <td>{{ customer.get_course_type_display}}</td>
            <td>{{ customer.consult_memo|truncatechars:50}}</td>
            <td class="{{ customer.status }}">{{ customer.status|alex_upper }}</td>
            <td>{{ customer.consultant}}</td>
            <td>{{ customer.date}}</td>
        </tr>
        {% endfor %}
        </tbody>
    </table>

    <div class="pagination">
        <nav>
          <ul class="pagination">
            {% if customer_list.has_previous %}
                <li class="">
                    <a href="?page={{ customer_list.previous_page_number }}" aria-label="Previous">
                        <span aria-hidden="true">&laquo;</span>
                    </a>
                </li>
            {% endif  %}
              {% for page_num in customer_list.paginator.page_range %}
                   {%  guess_page customer_list.number page_num  %}
            {% endfor %}
           {% if customer_list.has_next %}
                <li class=""><a href="?page={{ customer_list.next_page_number }}" aria-label="Next"><span aria-hidden="true">&raquo;</span></a></li>
           {% endif %}
          </ul>
        </nav>


    </div>
{% endblock %}
配置html显示数据
def customers(request):
    customer_list = models.Customer.objects.all()
    paginator = Paginator(customer_list,3)
    page = request.GET.get('page')
    try:
        customer_objs = paginator.page(page)
    except PageNotAnInteger:
        customer_objs = paginator.page(1)
    except EmptyPage:
         customer_objs = paginator.page(paginator.num_pages)
    return render(request,'crm/customers.html',{'customer_list':customer_objs})
通过views获取数据显示

 分页代码

    <div class="pagination">
        <nav>
          <ul class="pagination">
            {% if customer_list.has_previous %}
                <li class="">
                    <a href="?page={{ customer_list.previous_page_number }}" aria-label="Previous">
                        <span aria-hidden="true">&laquo;</span>
                    </a>
                </li>
            {% endif  %}
              {% for page_num in customer_list.paginator.page_range %}
                   {%  guess_page customer_list.number page_num  %}
            {% endfor %}
           {% if customer_list.has_next %}
                <li class=""><a href="?page={{ customer_list.next_page_number }}" aria-label="Next"><span aria-hidden="true">&raquo;</span></a></li>
           {% endif %}
          </ul>
        </nav>


    </div>
前端展示
from django import template
from django.utils.html import format_html

register = template.Library()

@register.filter
def alex_upper(val):
    print("--val from template:",val )
    return val.upper()


@register.simple_tag
def guess_page(current_page,loop_num):
    offset = abs(current_page - loop_num)
    if offset <3:
        if current_page == loop_num:
            page_ele = '''<li class="active"><a href="?page=%s">%s</a></li>''' %(loop_num,loop_num)
        else:
            page_ele = '''<li class=""><a href="?page=%s">%s</a></li>''' %(loop_num,loop_num)
        return format_html(page_ele)
    else:
        return ''
使页码滚动起来

通用权限系统的设计思路:

 加权限是因为希望不同的角色有不同的权限,不同的角色做的事情是不一样的。

posted @ 2016-07-10 10:36  Peony_Y  阅读(349)  评论(0编辑  收藏  举报