django组合搜索(搜索标签存数据库中)

models.py

from __future__ import unicode_literals
from django.db import models

# Create your models here.

class Category(models.Model):
    caption = models.CharField(max_length=12)
    def __unicode__(self):
        return self.caption

class ArticleTag(models.Model):
    caption = models.CharField(max_length=12)
    def __unicode__(self):
        return self.caption

class Article(models.Model):
    title = models.CharField(max_length=32, unique=True)
    content = models.TextField()
    author = models.CharField(max_length=32)
    category = models.ForeignKey(Category)
    article_tag = models.ForeignKey(ArticleTag)
    def __unicode__(self):
        return self.title

urls.py

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

urlpatterns = [
    url(r'^admin/', admin.site.urls),
#这里的<...>命名技巧为  @数据库的字段名_id@ ,以后数据库更新..objects.update(**kwargs)
    url(r'^index-(?P<category_id>\d+)-(?P<article_tag_id>\d+).html', views.index, name='index'),
]

views.py

from django.shortcuts import render
from app01 import models
# Create your views here.

def index(request, *args, **kwargs):
    # print(kwargs) 这里kwargs是一个字典,例{'category_id':'1', 'article_tag_id':'2'}
#但是数据库id中没有0,得重新构造一下字典
    condition = {}
    for k, v in kwargs.items():
        kwargs[k] = int(v)  #kwargs为当前页页码传入模板,最好把v转换为int格式,因为从urls.py 传过来时是一个字符串格式,模板的category_id这些都是一个int格式
        if v == '0':
            pass          #页码为0时,什么都不用干
        else:
            condition[k] = v   #页码不为0时开始..

    categorys = models.Category.objects.all()
    article_tags = models.ArticleTag.objects.all()
    articles = models.Article.objects.filter(**condition)
    return render(request, 'index.html', {'articles': articles, 'categorys': categorys, 'article_tags': article_tags, 'kwargs': kwargs})

tamplate-index.html

<!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: red;
       }
    </style>
</head>
<body>
<div class="condition">
<!--如果当前页为0,添加class="active" -->
{% if kwargs.category_id == 0 %}
    <a class="active" href="/index-0-{{ kwargs.article_tag_id }}.html">全部</a>
{% else %}
    <a href="/index-0-{{ kwargs.article_tag_id }}.html">全部</a>
{% endif %}
<!--如果当前页等于点击的那页, 添加class="active",其他的不添加-->
{% for c in categorys %}
    {% if c.id == kwargs.category_id %}
        <a class="active" href="/index-{{ c.id }}-{{ kwargs.article_tag_id }}.html">{{ c }}</a>
    {% else %}
        <a href="/index-{{ c.id }}-{{ kwargs.article_tag_id }}.html">{{ c }}</a>
    {% endif %}
{% endfor %}

<br>
<!-- 下同-->
{% if kwargs.article_tag_id == 0 %}
    <a class="active" href="/index-{{ kwargs.category_id }}-0.html">全部</a>
{% else %}
    <a href="/index-{{ kwargs.category_id }}-0.html">全部</a>
{% endif %}

{% for tag in article_tags %}
    {% if tag.id == kwargs.article_tag_id %}
        <a class="active" href="/index-{{ kwargs.category_id }}-{{ tag.id }}.html">{{ tag }}</a>
    {% else %}
        <a href="/index-{{ kwargs.category_id }}-{{ tag.id }}.html">{{ tag }}</a>
    {% endif %}
{% endfor %}
<!-- ======================根据上面的请求,传过来的articles也不同========================-->
{% for article in articles %}
    <h2>{{ article.title }}</h2>
    <span>{{ article.author }} {{ article.category.caption }} {{ article.article_tag.caption }}</span>
    <p>{{ article.content }}</p>
    <hr>
{% endfor %}
</div>
</body>
</html>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • (一)、启动服务器 (二)、创建数据库表 或 更改数据库表或字段 Django 1.7.1及以上 用以下命令 1....
    夏天夏星阅读 11,064评论 0 17
  • 此段内容简要来自自强学堂的教程详情请查询自强学堂 一、 后台的运作流程 接收request请求 处理数据 获取请求...
    coder_ben阅读 10,636评论 6 56
  • 去年的事情特别多,也没有什么时间充电学习。今年目测轻松一点,年初本来计划就好好休息一下,结果一晃2017就度过了一...
    灰豹儿阅读 3,818评论 0 2
  • 本博客是记录跟从慕课网课程所记下的笔记,更多内容请访问慕课网慕课网--项目源码 新建项目 打开命令行,进入到打算打...
    小白猿阅读 6,955评论 1 10
  • 经过对django的初步学习,我们已经对后台的基本流程以及django的运作有了一定的了解,但是这还不足够,dja...
    coder_ben阅读 9,275评论 8 34

友情链接更多精彩内容