{% 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>
models
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) # article_type = models.ForeignKey(ArticleType) type_choice = ( (1,'Python'), (2,'OpenStack'), (3,'Linux'), ) article_type_id = models.IntegerField(choices=type_choice)
views
from django.shortcuts import render
from app01 import models
def article(request,*args,**kwargs):
print(kwargs)
# print(request.path_info) # 获取当前URL
# from django.urls import reverse
# # {'article_type_id': '0', 'category_id': '0'}
# url = reverse('article',kwargs={'article_type_id': '1', 'category_id': '0'})
# print(url)
# print(kwargs) # {'article_type_id': '0', 'category_id': '0'}
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
}
)
templatetags/filter
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 %}
全部
{% else %}
全部
{% endif %}
:return:
"""
if k == 'article_type_id':
n1 = arg_dict['article_type_id']
n2 = arg_dict['category_id']
if n1 == 0:
ret = '全部' % n2
else:
ret = '全部' % n2
else:
n1 = arg_dict['category_id']
n2 = arg_dict['article_type_id']
if n1 == 0:
ret = '全部' % n2
else:
ret = '全部' % 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 %}
{{ row.caption }}
{% endif %}
{% endfor %}
:return:
"""
ret = []
for row in article_type_list:
if row[0] == arg_dict['article_type_id']:
temp = '%s' %(row[0],arg_dict['category_id'],row[1],)
else:
temp = '%s' %(row[0],arg_dict['category_id'],row[1],)
ret.append(temp)
return mark_safe(''.join(ret))
urls
"""s14day25 URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/1.10/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.conf.urls import url, include
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url
from django.contrib import admin
from app01 import views
from app02 import views as a2
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^req/', a2.req),
url(r'^article-(?P\d+)-(?P\d+).html',views.article,name='article'),
]