• 组合搜索


    from app1 import models
    def article(request,**kwargs):
    
        # from django.urls import reverse
        # url=reverse('article',kwargs=kwargs)
        # print(url)
    
    
        condition = {}
        for k,v in kwargs.items():
            kwargs[k]=int(v)
            print(k,v)
            if v == '0':
                pass
            else:
                condition[k]=v
                print(condition)
    
        article_type_list=models.ArticleType.objects.all()
        category_list=models.Category.objects.all()
        results=models.Article.objects.filter(**condition)
    
        return render(request,'article.html',
                      {'results':results,
                       'article_type_list':article_type_list,
                       'category_list':category_list,
                       'arg_dict':kwargs})
    views.py
    from django.db import models
    
    # Create your models here.
    class Article(models.Model):
    
        title=models.CharField(max_length=32)
        content=models.TextField(max_length=255)
        article_type=models.ForeignKey(to='ArticleType',on_delete='on_delete')
        category=models.ForeignKey(to='Category',on_delete='on_delete')
    
    
    class ArticleType(models.Model):
        caption=models.CharField(max_length=16)
    
    
    class Category(models.Model):
        caption = models.CharField(max_length=16)
    models.py
    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <style>
            .c1 {
    
                display: inline-block;
                background-color: mediumvioletred;
                padding: 5px;
                margin: 5px;
    
            }
    
            .active{
                background-color: #0e90d2;
    
            }
    
        </style>
    
    </head>
    <body>
    
    {% include "tag.html" %}
    {{ page }}
    
    
    </body>
    </html>
    html
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    
        <style>
            .condition a {
    
                display: inline-block;
                padding: 3px 5px;
                border: solid 1px red;
                margin: 5px 5px;
    
            }
              .condition a.active {
    
               background-color: #0e90d2;
    
            }
    
        </style>
    
    </head>
    <body>
    
    <h1>过滤条件</h1>
    <div class="condition">
    
    <div>
    <a href="/article-0-{{ arg_dict.article_type_id }}.html">全部</a>
    
    
    {% 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>
    
    <a href="/article-{{ arg_dict.category_id }}-0.html">全部</a>
    
    
    {% 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>
    {% for row in results %}
    
        <li>{{ row.id }}-{{ row.title }}</li>
        <p style="background-color: #0e90d2"><span>{{ row.content}}</span></p>
    
    {% endfor %}
    
    {% for item in test %}
    <p>{{ item.content }}</p>
    
    {% endfor %}
    
    </body>
    </html>
    article.html
  • 相关阅读:
    Python shutil模块
    configparser模块来生成和修改配置文件
    用random模块实现验证码
    python 正则re模块
    python 装饰器
    python迭代器和生成器
    python替换一个文件里面的特定内容
    广告资源收集
    Java 语言中 Enum 类型的使用介绍
    FreeMarker + xml 导出word
  • 原文地址:https://www.cnblogs.com/sunhao96/p/9036377.html
Copyright © 2020-2023  润新知