django 添加自定义context
文档参考;http://python.usyiyi.cn/django_182/ref/templates/api.html
添加自定义上下文文件custom_processors.py
# coding=utf-8 from .models import Article,Category
from django.db.models import Count def month_list(request):
articles = Article.objects.all()
year_month = set()
for a in articles:
year_month.add((a.cre_date.year,a.cre_date.month))
counter = {}.fromkeys(year_month,0)
for a in articles:
counter[(a.cre_date.year,a.cre_date.month)]+=1
year_month_number = []
for key in counter:
year_month_number.append([key[0],key[1],counter[key]])
year_month_number.sort(reverse=True)
return {'year_month_number': year_month_number} def category(request):
category = Category.objects.annotate(num_article=Count('article'))
return {"categories": category}
更在setting.py文件
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'blog.custom_processors.month_list',
'blog.custom_processors.category'
],
},
},
]
可以在前端使用year_month_number和categories
django 添加自定义context的更多相关文章
- Django的Context和RequestContext
参考:http://www.dannysite.com/blog/38/ Django的模板渲染中,Context可以用来传递数据,一个Context是一系列变量和值的集合,它和Python的字典有点 ...
- django 模板context的理解
context作为view与template之间的桥梁,理解它的工作原理对于djagno的模板工作机制至关重要. class ContextDict(dict):#上下文词典,由词典可以通过conte ...
- Django 添加自定义包路径
在设置文件里: import sys sys.path.insert(0,os.path.join(BASE_DIR,"要导包的目录名")) 用pycharm时,如果导包后没有自动 ...
- 自定义django的Template context processors
简要步骤: 1.编辑一个函数: def media_url(request): from django.conf import settings return {'media_url': settin ...
- Django context must be a dict ranther than Context
1.1 错误描述 TypeError at /time/ context must be a dict rather than Context. Request Method: GET Request ...
- Django进阶篇(一)
Form django中的Form一般有两种功能: 1.输入html 2.验证用户输入 最简易的form验证: <!DOCTYPE html> <html lang="en ...
- Django
一.Django 简介 Django 是一个由 Python 写成的开放源代码的 Web 应用框架.它最初是被开发来用于管理劳伦斯出版集团旗下的一些以新闻内容为主的网站的,即是 CMS(内容管理系统) ...
- django之一些feature
前端之django一些feature 本节内容 cookie session 跨站请求保护 分页 序列化 model模块 CBV和FBV 模板渲染对象 1. cookie cookie 是一种发送到客 ...
- django 缓存、中间件、信号、CSRF 详解
中间件 django 中的中间件(middleware),在django中,中间件其实就是一个类,在请求到来和结束后,django会根据自己的规则在合适的时机执行中间件中相应的方法. 在django项 ...
随机推荐
- Ubuntu 16.04安装各种软件
Ubuntu 16.04发布了,带来了很多新特性,同样也依然带着很多不习惯的东西,所以装完系统后还要进行一系列的优化. 1.删除libreoffice libreoffice虽然是开源的,但是Java ...
- 父标签浮动(float)“塌陷”问题
浮动“塌陷” float参见: http://www.cnblogs.com/bigtreei/p/8110090.html http://www.w3school.com.cn/css/css_po ...
- boost之数学与数字
1.random随机数产生,需要种子,下面以时间为种子示例: #include <iostream> #include <string> #include <vector ...
- List contents of directories in a tree-like format
Python programming practice. Usage: List contents of directories in a tree-like format. #!/usr/bin/p ...
- JAVA学习笔记----【转】 java.toString() ,(String),String.valueOf的区别
在java项目的实际开发和应用中,常常需要用到将对象转为String这一基本功能.本文将对常用的转换方法进行一个总结. 常用的方法有Object#toString(),(String)要转换的对象,S ...
- Centos(Yum源更改)
第一步:备份你的原镜像文件,以免出错后可以恢复. [root@openstack yum.repos.d]#mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum. ...
- 06_Hadoop配置伪分布式模式详解
查看IP地址,设为手动模式: 配置hadoop用户sudo权限 su切换到root身份,配置vim /etc/sudoers文件,加入 hadoop ALL=(root)NOPASSWD:ALL ...
- 40个你可能不知道的Python的特点和技巧
1.拆箱 >>> a, b, c = 1, 2, 3 >>> a, b, c (1, 2, 3) >>> a, b, c = [1, 2, 3] ...
- 【HackerRank】Closest Numbers
Sorting is often useful as the first step in many different tasks. The most common task is to make f ...
- 【CodeChef】Factorial(n!末尾0的个数)
The most important part of a GSM network is so called Base Transceiver Station (BTS). These transcei ...