码迷,mamicode.com
首页 > 其他好文 > 详细

搜索功能开发

时间:2019-08-09 01:06:41      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:append   过滤   utils   model   图片   %s   username   dir   lse   

input标签 type="hidden" 隐藏的标签

 

from django.db.models import Q

q=Q()
q.connector=‘OR‘

q.children.append((条件))

querysets.filter(q)  或的筛选

技术图片
from django.template import Library
from django.utils.safestring import mark_safe
import datetime ,time
register = Library()

@register.simple_tag
def build_filter_ele(filter_column,admin_class):

    column_obj = admin_class.model._meta.get_field(filter_column)
    print("column obj:",column_obj)
    try:
        #filter_column_name = "<span>%s</span>" % filter_column
        filter_ele = "<div class=‘col-md-2‘>%s<select class=‘form-control ‘ name=‘%s‘>" % (filter_column,filter_column)
        for choice in column_obj.get_choices():
            selected = ‘‘
            if filter_column in admin_class.filter_condtions:#当前字段被过滤了
                # print("filter_column", choice,
                #       type(admin_class.filter_condtions.get(filter_column)),
                #       admin_class.filter_condtions.get(filter_column))
                if str(choice[0]) == admin_class.filter_condtions.get(filter_column):#当前值被选中了
                    selected = selected
                    print(selected......)

            option = "<option value=‘%s‘ %s>%s</option>" % (choice[0],selected,choice[1])
            filter_ele += option
    except AttributeError as e:
        print("err",e)
        filter_ele = "<div class=‘col-md-2‘><select  class=‘form-control‘ name=‘%s__gte‘>" % filter_column
        if column_obj.get_internal_type() in (DateField,DateTimeField):
            time_obj = datetime.datetime.now()
            time_list = [
                [‘‘,------],
                [time_obj,Today],
                [time_obj - datetime.timedelta(7),七天内],
                [time_obj.replace(day=1),本月],
                [time_obj - datetime.timedelta(90),三个月内],
                [time_obj.replace(month=1,day=1),YearToDay(YTD)],
                [‘‘,ALL],
            ]

            for i in time_list:
                selected = ‘‘
                time_to_str = ‘‘if not i[0] else  "%s-%s-%s"%(i[0].year,i[0].month,i[0].day)
                if  "%s__gte"% filter_column in admin_class.filter_condtions:  # 当前字段被过滤了
                    print(-------------gte)
                    if time_to_str == admin_class.filter_condtions.get("%s__gte"% filter_column):  # 当前值被选中了
                        selected = selected
                option = "<option value=‘%s‘ %s>%s</option>" %                          (time_to_str ,selected,i[1])
                filter_ele += option

    filter_ele += "</select></div>"
    return mark_safe(filter_ele)



@register.simple_tag
def  build_table_row(obj,admin_class):
    """生成一条记录的html element"""

    ele = ""
    if admin_class.list_display:
        for column_name in admin_class.list_display:

            column_obj = admin_class.model._meta.get_field(column_name)
            if column_obj.choices: #get_xxx_display
                column_data = getattr(obj,get_%s_display% column_name)()
            else:
                column_data = getattr(obj,column_name)

            td_ele = "<td>%s</td>"% column_data
            ele += td_ele
    else:
        td_ele = "<td>%s</td>" % obj

        ele += td_ele
    return mark_safe(ele)



@register.simple_tag
def get_model_name(admin_class):
    return admin_class.model._meta.model_name.upper()



@register.simple_tag
def get_sorted_column(column,sorted_column,forloop):
    #sorted_column = {‘name‘: ‘-0‘}
    if column in sorted_column:#这一列被排序了,
        #你要判断上一次排序是什么顺序,本次取反
        last_sort_index = sorted_column[column]
        if last_sort_index.startswith(-):
            this_time_sort_index = last_sort_index.strip(-)
        else:
            this_time_sort_index = -%s % last_sort_index
        return this_time_sort_index
    else:
        return forloop


@register.simple_tag
def render_filtered_args(admin_class,render_html=True):
    ‘‘‘拼接筛选的字段‘‘‘
    if admin_class.filter_condtions:
        ele = ‘‘
        for k,v in admin_class.filter_condtions.items():
            ele += &%s=%s %(k,v)
        if render_html:
            return mark_safe(ele)
        else:
            return ele
    else:
        return ‘‘


@register.simple_tag
def render_sorted_arrow(column,sorted_column):
    if column in sorted_column:  # 这一列被排序了,
        last_sort_index = sorted_column[column]
        if last_sort_index.startswith(-):
            arrow_direction = bottom
        else:
            arrow_direction = top
        ele = ‘‘‘<span class="glyphicon glyphicon-triangle-%s" aria-hidden="true"></span>‘‘‘ % arrow_direction
        return mark_safe(ele)
    return ‘‘


@register.simple_tag
def render_paginator(querysets,admin_class,sorted_column):
    ele = ‘‘‘
      <ul class="pagination">
    ‘‘‘
    for i in querysets.paginator.page_range:
        if abs(querysets.number - i) < 2 :#display btn
            active = ‘‘
            if querysets.number == i : #current page
                active = active
            filter_ele = render_filtered_args(admin_class)

            sorted_ele = ‘‘
            if sorted_column:
                sorted_ele = &_o=%s % list(sorted_column.values())[0]

            p_ele = ‘‘‘<li class="%s"><a href="?_page=%s%s%s">%s</a></li>‘‘‘  % (active,i,filter_ele,sorted_ele,i)

            ele += p_ele


    ele += "</ul>"

    return mark_safe(ele)


@register.simple_tag
def get_current_sorted_column_index(sorted_column):

    return list(sorted_column.values())[0] if sorted_column else ‘‘
kingadmin_tags.py
技术图片
from django.shortcuts import render,redirect
from django.contrib.auth import authenticate,login,logout
from django.contrib.auth.decorators import login_required
from django import conf
from django.db.models import Q
from django.core.paginator import Paginator,PageNotAnInteger,EmptyPage
from kingadmin import app_setup
from crm import models
app_setup.kingadmin_auto_discover()


from kingadmin.sites import  site
print("sites.",site.enabled_admins)

# for k,v in site.enabled_admins.items():
#     for table_name,admin_class in v.items():
#         print(table_name,id(admin_class))
# # Create your views here.


def app_index(request):
    #enabled_admins =

    return render(request,kingadmin/app_index.html, {site:site})

def get_filter_result(request,querysets):
    filter_conditions = {}
    for key,val in request.GET.items():
        if key in (_page,_o,_q):continue
        if val:
            filter_conditions[key] =  val


    print("filter_conditions",filter_conditions)
    return querysets.filter(**filter_conditions),filter_conditions

def get_orderby_result(request,querysets,admin_class):
    """排序"""

    current_ordered_column = {}
    orderby_index = request.GET.get(_o)
    if orderby_index:
        orderby_key =  admin_class.list_display[ abs(int(orderby_index)) ]
        current_ordered_column[orderby_key] = orderby_index #为了让前端知道当前排序的列

        if orderby_index.startswith(-):
            orderby_key =  -+ orderby_key

        return querysets.order_by(orderby_key),current_ordered_column
    else:
        return querysets,current_ordered_column


def get_serached_result(request,querysets,admin_class):


    search_key = request.GET.get(_q)
    if search_key :
        q = Q()
        q.connector = OR

        for search_field in admin_class.search_fields:
            q.children.append(("%s__contains"% search_field,search_key))


        return  querysets.filter(q)
    return querysets




@login_required
def table_obj_list(request,app_name,model_name):
    """取出指定model里的数据返回给前端"""
    #print("app_name,model_name:",site.enabled_admins[app_name][model_name])
    admin_class = site.enabled_admins[app_name][model_name]
    querysets = admin_class.model.objects.all()

    querysets,filter_condtions  = get_filter_result(request,querysets)
    admin_class.filter_condtions = filter_condtions

    #searched queryset result
    querysets = get_serached_result(request,querysets,admin_class)
    admin_class.search_key = request.GET.get(_q,‘‘)

    #sorted querysets
    querysets,sorted_column = get_orderby_result(request,querysets,admin_class)


    paginator = Paginator(querysets, 2) # Show 25 contacts per page

    page = request.GET.get(_page)
    try:
        querysets = paginator.page(page)
    except PageNotAnInteger:
        # If page is not an integer, deliver first page.
        querysets = paginator.page(1)
    except EmptyPage:
        # If page is out of range (e.g. 9999), deliver last page of results.
        querysets = paginator.page(paginator.num_pages)
    print(request.GET)
    #print("admin class",admin_class.model )

    return render(request,kingadmin/table_obj_list.html, {querysets:querysets,
                                                            admin_class:admin_class,
                                                            sorted_column:sorted_column})


def acc_login(request):
    error_msg = ‘‘
    if request.method == "POST":
        username = request.POST.get(username)
        password = request.POST.get(password)

        user = authenticate(username=username,password=password)
        if user:
            print("passed authencation",user)
            login(request,user)
            #request.user = user

            return  redirect( request.GET.get(next,/kingadmin/) )
        else:
            error_msg = "Wrong username or password!"
    return render(request, kingadmin/login.html, {error_msg:error_msg})


def acc_logout(request):
    logout(request)
    return redirect("/login/")
views
技术图片
{% extends ‘kingadmin/index.html‘ %}
{% load kingadmin_tags %}

{% block  right-content-container %}
<h2 class="page-header">app</h2>

<div>

        {{ querysets }}
        <form>
            <input type="search" placeholder="{% for s in admin_class.search_fields %}{{ s }},{% endfor %}" name="_q" value="{{ admin_class.search_key }}">
            <input type="submit" value="Search">

            {% for k,v in admin_class.filter_condtions.items %}
                <input type="hidden" name="{{ k }}" value="{{ v }}">
            {% endfor %}
        </form>
        <div class="row">
            {% if  admin_class.list_filter %}
            <form >

                {% for filter_column in admin_class.list_filter %}

                   {% build_filter_ele filter_column admin_class %}
                {% endfor %}
                <input type="hidden" name="_o" value="{% get_current_sorted_column_index sorted_column %}">
                <input class="btn btn-success" type="submit" value="过滤">
            </form>
            {% endif %}

        </div>

        <table class="table table-striped">
            <thead>
                <tr>
                    {% if admin_class.list_display %}
                        {% for column in admin_class.list_display %}
                            <th><a href="?_o={% get_sorted_column column sorted_column forloop.counter0 %}{% render_filtered_args admin_class%}">
                                {{ column }}
                                {% render_sorted_arrow column  sorted_column %}
                            </a></th>

                        {% endfor %}
                    {% else %}
                        <th>{% get_model_name admin_class %}</th>
                    {% endif %}
                </tr>
            </thead>
            <tbody>
                {% for obj in querysets %}
                    <tr>{%  build_table_row obj admin_class %}</tr>
                {% endfor %}
            </tbody>





        </table>

        <div class="pagination">


            {% render_paginator querysets admin_class sorted_column %}

        </div>


</div>

{% endblock %}
table_obj_list.py

技术图片技术图片技术图片技术图片技术图片技术图片技术图片技术图片技术图片技术图片技术图片技术图片技术图片技术图片技术图片技术图片

搜索功能开发

标签:append   过滤   utils   model   图片   %s   username   dir   lse   

原文地址:https://www.cnblogs.com/jintian/p/11324542.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!