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项 ...
随机推荐
- exe4j中"this executable was created with an evaluation version of exe4j"
在使用exe4j时,如果您的exe4j没有注册,在运行有exe4j转换的*.jar为*.exe的可执行文件是会提示:"this executable was created with an ...
- 涉及到复制和二进制日志中的选项和变量-Replication and Binary Logging Options and Variables
在搭建复制中,有些参数需要我们留意,在这里罗列出来,供大家参考一下,以GTID为基础 --server-id server-id:这是一个全局的可动态调整的变量,取值范围为0-4294967295,也 ...
- ./bin/console server:run Surprise! There are no commands defined in the "server" namespace.
Let's start the built-in web server: ./bin/console server:run Surprise! There are no commands defi ...
- java ReentrantLock可重入锁的使用场景
摘要 从使用场景的角度出发来介绍对ReentrantLock的使用,相对来说容易理解一些. 场景1:如果发现该操作已经在执行中则不再执行(有状态执行) a.用在定时任务时,如果任务执行时间可能超过下次 ...
- 常用mysql导入导出数据的命令
To export 导出指定db_name的数据: $ mysqldump -u [uname] -p[pass] db_name > db_backup.sql 导出整个库的数据: $ mys ...
- admin 模块功能
class CustomModelAdmin(admin.ModelAdmin): def has_module_permission(self, request): 是否会显示model def h ...
- Django 进阶篇之 CSRF,COOIKE,SESSION
1.CSRF(Cross Site Request Forgery, 跨站域请求伪造) CSRF 背景与介绍 CSRF(Cross Site Request Forgery, 跨站域请求伪造)是一种网 ...
- c# 抽象类(abstract)
using System; using System.Collections.Generic; using System.Linq; using System.Text; //抽象类(abstract ...
- 常用的机器学习&数据挖掘知识点
Basis(基础):MSE(Mean Square Error 均方误差),LMS(LeastMean Square 最小均方),LSM(Least Square Methods 最小二乘法),MLE ...
- 主攻ASP.NET.4.5 MVC4.0之重生:图书推荐
前段时间看完ASP.Net4.0 框架揭秘 ,目前现在此书在家睡大觉,看得云里雾里,实战有些东西用不上,感觉好可惜. 大概看了一下这本书,这本书很多功能,可以在实际项目中能用上的方法和技巧.小小推荐, ...