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})
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)
<!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>
<!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>