• 六个关键点
    1.目标客群的确认和目标客痛点的确认
    2.满足目标客群的情况
    3如何跟目标客户群形成信任关系
    4.目标客户群能形成多少依赖关系
    5.目标客群对你提供的产品或服务的满意程度
    6.目标客户群持续购买的意愿和介绍客户的意愿

  • 三千年读史,不外功名利禄;九万里悟道,终归诗酒田园

  • 太多的人是二极管思维,一说国有就是好,一说民营就是坏. 把运营与监管问题,非得拉到企业性质上去.唉.

  • 难将由我,我不为难,谁敢兴之!”——《资治通鉴》
  • python脚本打包工具 pyinstaller nuitka pyportable-installer cx_freeze py2exe

  • 农村大三谚语:不杀跪地牛,不吃看门狗,不弃结发妻。 ​

  • 兔死狗烹、卸磨杀驴、鸟尽弓藏、藏弓烹狗、过河拆桥

  • 鸟尽弓藏 兔死狗烹 卸磨杀驴

  • 焚我残躯,熊熊圣火 生亦何欢,死亦何苦 为善除恶,唯光明故

祀与戎:柴少的博客:Django之组合查找功能(十七)

思想观点 siyurong 2个月前 (01-08) 8次浏览 已收录 0个评论

http://120ni.51niux.com/post/31.html

Django之组合查找功能(十七)

通过Django实现类似于如下的组合查找功能:图片.png

一、实现方式一

1.1  先试着拿到所有的内容

models.py :

Bash
from django.db import models
# Create your models here.
# 创建一个标签表
class Category(models.Model):
    caption = models.CharField(max_length=16)
    
#文章类型分类表动态的就要搞表
class ArticleType(models.Model):
     caption = models.CharField(max_length=16)

#创建一个标题和内容表
class Article(models.Model):
    title = models.CharField(max_length=32)
    content = models.CharField(max_length=255)
    # 建立外键约束
    category = models.ForeignKey(Category,on_delete=None)
    article_type = models.ForeignKey(ArticleType,on_delete=None)

# type_choice = (  #如果是静态的就存放到内存里面
# (1,'Python'),
# (2,'OpenStack'),
# (3,'Linux'),
# )
# article_type_id = models.IntegerField(choices=type_choice)

python manage.py makemigrations
python manage.py migrate

图片.png

#上面是往类型表里面插入点数据,一般类型就定死了很少变了。

图片.png

#上面是往分类表里面插入点数据,想写什么就写什么。

图片.png

#在内容表里面插入点内容。

urls.py :

Bash
url(r'^article/', views.article),

views.py:

Bash
from django.shortcuts import render
from app01 import models

def article(request):
    result = models.Article.objects.all()
    return  render(request,'article.html',{'result':result})

article.html:

Bash
<body>
    <h1>查询结果</h1>
    <ul>
    {% for row in result %}
        <li>{{ row.id }}-{{ row.title }}</li>
    {% endfor %}
    </ul>
</body>

测试一下:

图片.png

#这就把数据库里面所有的文章标题都拿到了。

1.2  多显示一点把分类什么的都打印出来

views.py:

Bash
from django.shortcuts import render
from app01 import models

def article(request,*args,**kwargs):
    article_type_list = models.ArticleType.objects.all()
    category_list = models.Category.objects.all()
    result = models.Article.objects.all()
    return  render(
        request,
        'article.html',
        {
            'result':result,
            'article_type_list':article_type_list,
            'category_list':category_list
        })

article.html:

Bash
<body>
    <h1>过滤条件</h1>
    <div>
        {% for row in article_type_list %}
            <a>{{ row.caption }}</a>
        {% endfor %}
    </div>
    <div>
        {% for row in category_list %}
            <a>{{ row.caption }}</a>
        {% endfor %}
    </div>
    <h1>查询结果</h1>
    <ul>
    {% for row in result %}
      <li>{{ row.id }}-{{ row.title }}</li>
    {% endfor %}
    </ul>
</body>

测试一下:

图片.png

1.3 通过跳转不同的url实现组合过滤

urls.py :

Bash
url(r'^article-(?P<article_type_id>\d+)-(?P<category_id>\d+).html', views.article),

views.py :

Bash
from django.shortcuts import render

# Create your views here.
from app01 import models
from django.urls import  reverse

def article(request,*args,**kwargs):
    print(kwargs)
    condition = {}
    for k,v in kwargs.items():
        kwargs[k] = int(v)
        if v == '0':
            pass
        else:
            condition[k] = v
    article_type_list = models.ArticleType.objects.all()
    category_list = models.Category.objects.all()
    result = models.Article.objects.filter(**condition)
    return  render(
        request,
        'article.html',
        {
            'result':result,
            'article_type_list':article_type_list,
            'category_list':category_list,
            'arg_dict':kwargs
        })

article.html:

Bash
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
        <style>
        .condition a{
            display: inline-block;
            padding: 3px 5px;
            border: 1px solid #dddddd;
            margin: 5px ;
        }
        .condition a.active{
            background-color: brown;
        }
        </style>
</head>
<body>
    <h1>过滤条件</h1>
    <div class="condition">
        <div>
            {% if arg_dict.article_type_id == 0  %}
                <a class="active" href="/article-0-{{ arg_dict.category_id }}.html">全部</a>
            {% else %}
                <a  href="/article-0-{{ arg_dict.category_id }}.html">全部</a>
            {% endif %}
            {% for row in article_type_list %}
                {% if row.id == arg_dict.article_type_id %}
                    <a class="active" href="/article-{{ row.id }}-{{ arg_dict.category_id }}.html">{{ row.caption }}</a>
                {% else %}
                    <a  href="/article-{{ row.id }}-{{ arg_dict.category_id }}.html">{{ row.caption }}</a>
                {% endif %}
            {% endfor %}
        </div>
        <div>
            {% if arg_dict.category_id == 0  %}
                <a class="active" href="/article-{{ arg_dict.article_type_id }}-0.html">全部</a>
            {% else %}
                <a  href="/article-{{ arg_dict.article_type_id }}-0.html">全部</a>
            {% endif %}
            {% for row in category_list %}
                {% if row.id == arg_dict.category_id %}
                    <a class="active" href="/article-{{ arg_dict.article_type_id }}-{{ row.id }}.html">{{ row.caption }}</a>
                {% else %}
                    <a  href="/article-{{ arg_dict.article_type_id }}-{{ row.id }}.html">{{ row.caption }}</a>
                {% endif %}
            {% endfor %}
        </div>
    </div>
    <h1>查询结果</h1>
    <ul>
    {% for row in result %}
      <li>{{ row.id }}-{{ row.title }}</li>
    {% endfor %}
    </ul>
</body>
</html>

测试一下:

图片.png

图片.png

#默认的url就是http://127.0.0.1:8000/article-0-0.html,这样就是显示全部信息。

图片.png

图片.png

#从上图可以看到内容已经发生了改变。

图片.png

图片.png

#可以看到组合筛选生效了。

二、实现方式二

方式一的代码中前端页面中有太多的判断,导致前端页面看起来比较杂乱,下面我们通过之前学习过的simple_tag的方式实现。

通过函数实现:app01/templatetags/filter.py:

Bash
from django import template
from django.utils.safestring import mark_safe  # 通过这个模块可以将html添加到前端页面
register = template.Library()  # 用户注册

@register.simple_tag
def filter_all(arg_dict, k):
    '''
    :param arg_dict:
    :param k: 用于判断输入的是article_type_id还是category_id
    :return:
    '''
    if k == "article_type_id":
        n1 = arg_dict[k]
        n2 = arg_dict["category_id"]
        if n1 == 0:
            ret = '<a class="active" href="/article-0-%s.html">全部</a>' % n2
        else:
            ret = '<a href="/article-0-%s.html">全部</a>' % n2
    else:
        n1 = arg_dict[k]
        n2 = arg_dict["article_type_id"]
        if n1 == 0:
            ret = '<a class="active" href="/article-%s-0.html">全部</a>' % n2
        else:
            ret = '<a href="/article-%s-0.html">全部</a>' % n2
    return mark_safe(ret)


@register.simple_tag
def filter_article_type(article_type_list, arg_dict):
    '''
    :param article_type_list:
    :param arg_dict:
    :return:
    '''
    ret = []
    for row in article_type_list:
        if row.id == arg_dict["article_type_id"]:
            temp = '<a class="active" href="/article-%s-%s.html">%s</a>' % (row.id, arg_dict["category_id"], row.caption)
        else:
            temp = '<a href="/article-%s-%s.html">%s</a>' % (row.id, arg_dict["category_id"], row.caption)
        ret.append(temp)
    return mark_safe("".join(ret))

@register.simple_tag
def filter_category(category_list, arg_dict):
    ret = []
    for row in category_list:
        if row.id == arg_dict["category_id"]:
            temp = '<a class="active" href="/article-%s-%s.html">%s</a>' % (arg_dict["article_type_id"], row.id, row.caption)
        else:
            temp = '<a href="/article-%s-%s.html">%s</a>' % (arg_dict["article_type_id"], row.id, row.caption)
        ret.append(temp)
    return mark_safe("".join(ret))

article.html :

Bash
{% load filter %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        .condition a{
            display: inline-block;
            padding: 3px 5px;
            border: 1px solid #dddddd;
            margin: 5px ;
        }
        .condition a.active{
            background-color: brown;
        }
    </style>
</head>
<body>
    <h1>过滤条件</h1>
    <div class="condition">
        <div>
            {% filter_all arg_dict 'article_type_id' %}
            {% filter_article_type article_type_list arg_dict %}
        </div>
        <div>
            {% filter_all arg_dict 'category_id' %}
            {% for row in category_list %}
                {% if row.id == arg_dict.category_id %}
                    <a class="active" href="/article-{{ arg_dict.article_type_id }}-{{ row.id  }}.html">{{ row.caption }}</a>
                {% else %}
                    <a  href="/article-{{ arg_dict.article_type_id }}-{{ row.id  }}.html">{{ row.caption }}</a>
                {% endif %}
            {% endfor %}
        </div>
    </div>
    <h1>查询结果</h1>
    <ul>
        {% for row in result %}
            <li>{{ row.id }}-{{ row.title }}</li>
        {% endfor %}
    </ul>
</body>
</html>

测试一下:

图片.png

作者:chaishaopeng 分类:Django学习 浏览:923 评论:0

祀与戎 , 版权所有蜀ICP备14023000号-6丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:祀与戎:柴少的博客:Django之组合查找功能(十七)
喜欢 (0)
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

Optimized by WPJAM Basic。