python Django学习(19)——实现组合搜索案例

model.py:

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="")
    # article_type = models.ForeignKey(ArticleType)

    type_choice = (
        (1,'Python'),
        (2,'OpenStack'),
        (3,'Linux'),
    )
    article_type_id = models.IntegerField(choices=type_choice)

views.py:

from django.shortcuts import render,HttpResponse,redirect
from app01 import models

# Create your views here.

def index(request):
    return HttpResponse('ok')

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()
    article_type_list = models.Article.type_choice
    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
        }
    )

urls.py:

from django.contrib import admin
from django.urls import path
from django.urls.conf import include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('app01/', include('app01.url')),
]

app01/url.py:

from django.urls import path,re_path
from app01 import views

urlpatterns = [
    path('index/',views.index ),
    re_path('article-(?P<article_type_id>\d+)-(?P<category_id>\d+).html',views.article ),
]

templatetags/filter.py(自定义simple_tag):

from django import template
from django.utils.safestring import mark_safe
register = template.Library()

@register.simple_tag
def filter_all(arg_dict,k):
    """
    {% 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 %}
    :return:
    """
    if k == 'article_type_id':
        n1 = arg_dict['article_type_id']
        n2 = arg_dict['category_id']
        if n1 == 0:
            ret = '<a class="active" href="/app01/article-0-%s.html">全部</a>' % n2
        else:
            ret = '<a href="/app01/article-0-%s.html">全部</a>' % n2
    else:
        n1 = arg_dict['category_id']
        n2 = arg_dict['article_type_id']
        if n1 == 0:
            ret = '<a class="active" href="/app01/article-%s-0.html">全部</a>' % n2
        else:
            ret = '<a href="/app01/article-%s-0.html">全部</a>' % n2

    return mark_safe(ret)

@register.simple_tag
def filter_article_type(article_type_list,arg_dict):
    """
    {% for row in article_type_list %}
        {% if row.id == arg_dict.article_type_id %}

        {% else %}
            <a  href="/article-{{ row.id  }}-{{ arg_dict.category_id }}.html">{{ row.caption }}</a>
        {% endif %}
    {% endfor %}
    :return:
    """
    ret = []
    for row in article_type_list:
        if row[0] == arg_dict['article_type_id']:
            temp = '<a class="active" href="/app01/article-%s-%s.html">%s</a>' %(row[0],arg_dict['category_id'],row[1],)
        else:
            temp = '<a href="/app01/article-%s-%s.html">%s</a>' %(row[0],arg_dict['category_id'],row[1],)
        ret.append(temp)
    return mark_safe(''.join(ret))

article.html:

{% 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="/app01/article-{{ arg_dict.article_type_id }}-{{ row.id  }}.html">{{ row.caption }}</a>
                {% else %}
                    <a href="/app01/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>
写在最后

    本文是个人的一些学习笔记,如有侵权,请及时联系我进行删除,谢谢大家.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值