django 添加自定义context

文档参考;http://python.usyiyi.cn/django_182/ref/templates/api.html

添加自定义上下文文件custom_processors.py

 1 # coding=utf-8
 2
 3 from .models import Article,Category
 4 from django.db.models import Count
 5
 6 def month_list(request):
 7     articles = Article.objects.all()
 8     year_month = set()
 9     for a in articles:
10         year_month.add((a.cre_date.year,a.cre_date.month))
11     counter = {}.fromkeys(year_month,0)
12     for a in articles:
13         counter[(a.cre_date.year,a.cre_date.month)]+=1
14     year_month_number = []
15     for key in counter:
16         year_month_number.append([key[0],key[1],counter[key]])
17     year_month_number.sort(reverse=True)
18     return {‘year_month_number‘: year_month_number}
19
20 def category(request):
21     category = Category.objects.annotate(num_article=Count(‘article‘))
22     return {"categories": category}

更在setting.py文件

 1 TEMPLATES = [
 2     {
 3         ‘BACKEND‘: ‘django.template.backends.django.DjangoTemplates‘,
 4         ‘DIRS‘: [],
 5         ‘APP_DIRS‘: True,
 6         ‘OPTIONS‘: {
 7             ‘context_processors‘: [
 8                 ‘django.template.context_processors.debug‘,
 9                 ‘django.template.context_processors.request‘,
10                 ‘django.contrib.auth.context_processors.auth‘,
11                 ‘django.contrib.messages.context_processors.messages‘,
12                 ‘blog.custom_processors.month_list‘,
13                 ‘blog.custom_processors.category‘
14             ],
15         },
16     },
17 ]

可以在前端使用year_month_number和categories

时间: 2024-08-27 10:39:53

django 添加自定义context的相关文章

Django的Context和RequestContext

参考:http://www.dannysite.com/blog/38/ Django的模板渲染中,Context可以用来传递数据,一个Context是一系列变量和值的集合,它和Python的字典有点相似. from django.template import Template,Context t = Template('My name is {{ name }}.') c = Context({'name':'Jacky'}) t.render(c) 在settings.py中有一个与Req

Python之路【第十七篇】:Django【进阶篇 】

Python之路[第十七篇]:Django[进阶篇 ] Model 到目前为止,当我们的程序涉及到数据库相关操作时,我们一般都会这么搞: 创建数据库,设计表结构和字段 使用 MySQLdb 来连接数据库,并编写数据访问层代码 业务逻辑层去调用数据访问层执行数据库操作 import MySQLdb def GetList(sql): db = MySQLdb.connect(user='root', db='wupeiqidb', passwd='1234', host='localhost')

Python开发【第二十二篇】:Web框架之Django【进阶】

Python开发[第二十二篇]:Web框架之Django[进阶] 猛击这里:http://www.cnblogs.com/wupeiqi/articles/5246483.html 博客园 首页 新随笔 联系 订阅 管理 随笔-124  文章-127  评论-205 Python之路[第十七篇]:Django[进阶篇 ] Model 到目前为止,当我们的程序涉及到数据库相关操作时,我们一般都会这么搞: 创建数据库,设计表结构和字段 使用 MySQLdb 来连接数据库,并编写数据访问层代码 业务逻

Django: 之Model、Cookis、Session

到目前为止,当我们的程序涉及到数据库相关操作时,我们一般都会这么搞: 创建数据库,设计表结构和字段 使用MySQLdb来连接数据库,并编写数据访问层代码 业务逻辑层去调用数据访问层执行数据库操作 import MySQLdb def GetList(sql): db = MySQLdb.connect(user='root', db='wulaoerdb', passwd='1234', host='localhost') cursor = db.cursor() cursor.execute(

Python之路【第十七篇】:Django之【进阶篇】

Python之路[第十七篇]:Django[进阶篇 ] Model 到目前为止,当我们的程序涉及到数据库相关操作时,我们一般都会这么搞: 创建数据库,设计表结构和字段 使用 MySQLdb 来连接数据库,并编写数据访问层代码 业务逻辑层去调用数据访问层执行数据库操作 import MySQLdb def GetList(sql): db = MySQLdb.connect(user='root', db='wupeiqidb', passwd='1234', host='localhost')

Django之Cookie、Session、CSRF

Cookie 1.获取Cookie: 1 2 3 4 5 6 request.COOKIES['key'] request.get_signed_cookie(key, default=RAISE_ERROR, salt='', max_age=None)     参数:         default: 默认值            salt: 加密盐         max_age: 后台控制过期时间 2.设置Cookie: 1 2 3 4 5 6 7 8 9 10 11 12 13 rep

Django之Model操作

Model 到目前为止,当我们的程序涉及到数据库相关操作时,我们一般都会这么搞: 创建数据库,设计表结构和字段 使用 MySQLdb 来连接数据库,并编写数据访问层代码 业务逻辑层去调用数据访问层执行数据库操作 import MySQLdb def GetList(sql): db = MySQLdb.connect(user='root', db='wupeiqidb', passwd='1234', host='localhost') cursor = db.cursor() cursor.

Python之路【第十七篇】:Django【进阶篇】

Python之路[第十七篇]:Django[进阶篇 ] Model 到目前为止,当我们的程序涉及到数据库相关操作时,我们一般都会这么搞: 创建数据库,设计表结构和字段 使用 MySQLdb 来连接数据库,并编写数据访问层代码 业务逻辑层去调用数据访问层执行数据库操作 import MySQLdb def GetList(sql): db = MySQLdb.connect(user='root', db='wupeiqidb', passwd='1234', host='localhost')

django的跨站请求访问

一.简介 django为用户实现防止跨站请求伪造的功能,通过中间件 django.middleware.csrf.CsrfViewMiddleware 来完成.而对于django中设置防跨站请求伪造功能有分为全局和局部. 全局: 中间件 django.middleware.csrf.CsrfViewMiddleware 局部: @csrf_protect,为当前函数强制设置仿跨站请求伪造功能,即便settings中没有设置全局中间件. @csrf_exempt,取消当前函数仿跨站请求伪造功能,即