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>