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),
#這里的<...>命名技巧為 @數(shù)據(jù)庫的字段名_id@ ,以后數(shù)據(jù)庫更新..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是一個(gè)字典,例{'category_id':'1', 'article_tag_id':'2'}
#但是數(shù)據(jù)庫id中沒有0电禀,得重新構(gòu)造一下字典
condition = {}
for k, v in kwargs.items():
kwargs[k] = int(v) #kwargs為當(dāng)前頁頁碼傳入模板幢码,最好把v轉(zhuǎn)換為int格式,因?yàn)閺膗rls.py 傳過來時(shí)是一個(gè)字符串格式,模板的category_id這些都是一個(gè)int格式
if v == '0':
pass #頁碼為0時(shí),什么都不用干
else:
condition[k] = v #頁碼不為0時(shí)開始..
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">
<!--如果當(dāng)前頁為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 %}
<!--如果當(dāng)前頁等于點(diǎn)擊的那頁, 添加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 %}
<!-- ======================根據(jù)上面的請(qǐng)求症副,傳過來的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>