django組合搜索(搜索標(biāo)簽存內(nèi)存中)

models.py

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models

# Create your models here.

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)
    cate_types = (
        (1, 'smartArticle'),
        (2, 'urlArticle'),
        (3, 'farmArticle'),
        (4, 'cookerArticle'),
    )
    category = models.IntegerField(choices=cate_types)
    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),
#注意這里<...>中的命名
    url(r'^article-(?P<category>\d+)-(?P<article_tag_id>\d+).html', views.article, name='article'),
]

views.py

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

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


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
    print condition
    articles = models.Article.objects.filter(**condition)
#注意存內(nèi)存中的輸出到模板這樣。。
    cate_types_list = models.Article.cate_types
    article_tags = models.ArticleTag.objects.all()
    return render(request, 'article.html', {'articles': articles, 'article_tags': article_tags, 'cate_types': cate_types_list, 'kwargs': kwargs})

template-article.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        .cc a{
            display: inline-block;
            border: double 2px;
            margin: 10px;
        }
        .active{
            background-color: aqua;
        }
    </style>
</head>
<body>

<div class="cc">
{% if kwargs.article_tag_id == 0 %}
<a  class="active" href="/article-{{ kwargs.category }}-0.html">全部</a>
{% else %}
<a href="/article-{{ kwargs.category }}-0.html">全部</a>
{% endif %}

{% for tag in article_tags %}
    {% if kwargs.article_tag_id == tag.id %}
        <a class="active" href="/article-{{ kwargs.category }}-{{ tag.id }}.html">{{ tag }}</a>
    {% else %}
        <a href="/article-{{ kwargs.category }}-{{ tag.id }}.html">{{ tag }}</a>
    {% endif %}
{% endfor %}

<br>

{% if kwargs.category == 0 %}
    <a class="active" href="/article-0-{{ kwargs.article_tag_id }}.html">全部</a>
{% else %}
    <a href="/article-0-{{ kwargs.article_tag_id }}.html">全部</a>
{% endif %}
<!-- 注意下面寫法,c.0-->
{% for c in cate_types %}
    {% if kwargs.category == c.0 %}
        <a class="active" href="/article-{{ c.0 }}-{{ kwargs.article_tag_id }}.html">{{ c.1 }}</a>
    {% else %}
        <a href="/article-{{ c.0 }}-{{ kwargs.article_tag_id }}.html">{{ c.1 }}</a>
    {% endif %}
{% endfor %}

{% for article in articles %}
    <h2>{{ article.title }}</h2>
    <span>{{ article.author }}&nbsp;---{{ article.article_tag }}&nbsp;---{{ article.get_category_display }}</span> <!-- 這里注意, 在模板打印存在內(nèi)存的標(biāo)簽寫法-->
    <p>{{ article.content }}</p>
{% endfor %}
</div>
</body>
</html>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容