文档参考;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的更多相关文章

  1. Django的Context和RequestContext

    参考:http://www.dannysite.com/blog/38/ Django的模板渲染中,Context可以用来传递数据,一个Context是一系列变量和值的集合,它和Python的字典有点 ...

  2. django 模板context的理解

    context作为view与template之间的桥梁,理解它的工作原理对于djagno的模板工作机制至关重要. class ContextDict(dict):#上下文词典,由词典可以通过conte ...

  3. Django 添加自定义包路径

    在设置文件里: import sys sys.path.insert(0,os.path.join(BASE_DIR,"要导包的目录名")) 用pycharm时,如果导包后没有自动 ...

  4. 自定义django的Template context processors

    简要步骤: 1.编辑一个函数: def media_url(request): from django.conf import settings return {'media_url': settin ...

  5. 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 ...

  6. Django进阶篇(一)

    Form django中的Form一般有两种功能: 1.输入html 2.验证用户输入 最简易的form验证: <!DOCTYPE html> <html lang="en ...

  7. Django

    一.Django 简介 Django 是一个由 Python 写成的开放源代码的 Web 应用框架.它最初是被开发来用于管理劳伦斯出版集团旗下的一些以新闻内容为主的网站的,即是 CMS(内容管理系统) ...

  8. django之一些feature

    前端之django一些feature 本节内容 cookie session 跨站请求保护 分页 序列化 model模块 CBV和FBV 模板渲染对象 1. cookie cookie 是一种发送到客 ...

  9. django 缓存、中间件、信号、CSRF 详解

    中间件 django 中的中间件(middleware),在django中,中间件其实就是一个类,在请求到来和结束后,django会根据自己的规则在合适的时机执行中间件中相应的方法. 在django项 ...

随机推荐

  1. Python迭代器包itertools(转)

    原文:http://www.cnblogs.com/vamei/p/3174796.html 作者:Vamei 在循环对象和函数对象中,我们了解了循环器(iterator)的功能.循环器是对象的容器, ...

  2. MySQL错误日志提示innodb_table_stats和innodb_index_stats不存在故障处理

    查看MySQL error日志,发现有如下报错 7efbc586f700 InnoDB: Error: Table "mysql"."innodb_table_stats ...

  3. 003-ARP地址解析协议

    一.概念 地址解析协议,即ARP(Address Resolution Protocol),是根据IP地址获取物理地址的一个TCP/IP协议.主机发送信息时将包含目标IP地址的ARP请求广播到网络上的 ...

  4. .NET Framework 3.5-8 下载地址

    https://dotnet.microsoft.com/download/dotnet-framework Version Released End of life .NET Framework 4 ...

  5. Java基础—复用类

    复用代码是Java众多引人注目的功能之一. 一般而言,实现代码重用java提供了两种方式:组合以及继承. 组合:新的类由现有类的对象所组成.(复用现有代码的功能,而非它的形式) 继承:按照现有类的类型 ...

  6. 如何配置一个路径,能够既适合Linux平台,又适合Windows平台,可以从这个路径中读取文件

    如何配置一个路径,能够既适合Linux平台,又适合Windows平台,可以从这个路径中读取文件? 目的:就是希望在项目的配置文件中配上一样的路径,不管协作者使用的是什么平台,都能够读到文件. 比如:L ...

  7. substring splice

    返回start到end之前 不包括end stringObject.substring(start,end) (不接受负数) stringObject.slice(start,end) start起始 ...

  8. Spring AOP 学习(一) 代理模式

    AOP为Aspect Oriented Programming的缩写,意为:面向切面编程(也叫面向方面),可以通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添加功能的一种技术. ...

  9. npm安装出错Unexpected end of input at 1:2307

    执行命令: npm cache clean --force 然后再安装 搞定

  10. Raspberry Pi开发之旅-土壤湿度检测

    一.土壤传感器 传感器四个针脚:  针脚 含义 AO 模拟信号输出 DO 数字信号输出 GND 电源负极 VCC 电源正极 二.接线 YL-38和YL69 之间直接用2根母对母线连接. YL-38和树 ...