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项 ...
随机推荐
- docker openvas
https://hub.docker.com/r/mikesplain/openvas/ Requirements DockerPorts available: 443, 9390, 9391 Usa ...
- java 对象与二进制互转
来自 : http://blog.csdn.net/luckyzhoustar/article/details/50402427 /** * @FileName: ByteToObject.java ...
- java基础:父类与子类之间变量和方法的调用
1)父类构造函数 java中当调用某个类的构造方法的时候,系统总会调用父类的非静态初始化块进行初始化,这个调用是隐式的,而且父类的静态初始化代码 块总是会被执行,接着调用父类的一个或者多个构造器执行初 ...
- 2、Python request、BeautifulSoup(download mm_pic)
import requests from bs4 import BeautifulSoup import os class DownLoadImg(object): def __init__(self ...
- 0107-将Monolith重构为微服务
重构到微服务的概述 将单一应用程序转换为微服务的过程是应用程序现代化的一种形式.这是开发人员几十年来一直在做的事情.因此,在将应用程序重构为微服务时,我们可以重用一些想法. 一个不使用的策略是重写“B ...
- 字符串之strcmp
功能:比较两个字符串的ascII码大小 输入:两个字符串 返回值:相等为0,大于为大于零,小于为小于零 #include <iostream> #include <assert.h& ...
- ANSI编码——代码页
详见wiki: http://zh.wikipedia.org/wiki/%E4%BB%A3%E7%A0%81%E9%A1%B5
- 手机端的META差异
手机端的META你了解多少? 我们先来简单了解下meta标签:meta指元素可提供有关页面的元信息(meta-information),比如针对搜索引擎和更新频度的描述和关键词. 标签位于文档的头部, ...
- 仿Facebook登录表单
在线演示 本地下载
- 20145240 《Java程序设计》第九周学习总结
20145240 <Java程序设计>第九周学习总结 教材学习内容总结 JBDC是用于执行SQL的解决方案,开发人员使用JDBC的标准接口,数据库厂商对接口直接操作,开发人员无须接触底层数 ...