(六)charper3-Extending Your Blog Application之djangoByExample阅读笔记
来源:互联网 发布:域名为什么会被墙 编辑:程序博客网 时间:2024/05/16 14:21
开发环境:ubuntu14.10 python3.5 django1.8
本篇要点:
• Creating custom template tags and filters
• Adding a sitemap and a post feed (暂时不写)
• Building a search engine with Solr and Haystack(暂时不写)
1、自定义template 标签:(1+2步):
PS:自定义的template标签有三种类型:
• simple_tag : Processes the data and returns a string
• inclusion_tag : Processes the data and returns a rendered template
• assignment_tag : Processes the data and sets a variable in the context
处理方法大同小异:
zero:建好目录和文件:在blog的这个app应用下新建templatetags文件夹,在该文件夹下,新建__init .py(init右边还要两下滑线,markdown显示不出来),再新建blog_tags.py(主要在这个文件中编辑新标签)
准备工作就绪,开始干活:
simple_tag-first:在blog_tags.py中添加:
from django import templatefrom ..models import Postregister = template.Library()#这里的simple_tag可以理解为django对自定义标签的分类之一@register.simple_tag#这里的total_posts是会在html中使用的标签名def total_posts(): return Post.published.count()
simple_tag-second:在html中使用标签,这里是在/blog/templates/blog/base.html中添加:
<p>This is my blog. I've written {% total_posts %} posts so far.</p>
总体来说,自定义标签使用还是挺方便的
0 0
- (六)charper3-Extending Your Blog Application之djangoByExample阅读笔记
- (七)charper4-Building a Social Website之djangoByExample阅读笔记
- chapter 26 Extending Your Application
- (三)开发之tango_with_django和djangoByExample阅读笔记(Ubuntu下开发)
- (五)djangoByExample阅读笔记(续(三))
- PTAMM阅读笔记之阶段性总结(六)
- 六级之阅读理解笔记2
- soft raid5阅读笔记之六--修复
- 【学习ARToolkit小记之六】 开发第一个ARToolkit程序(Developing your First Application)
- mysql阅读笔记六
- 阅读笔记(六)
- 简单Blog项目笔记之六:文章增删
- AMD OpenCL例子阅读笔记系列之Radix_Sort(六)
- Head First Design Patterns 阅读笔记之六: Command Pattern
- Dessecting MFC 阅读笔记 六
- 论文阅读笔记(六)
- Extending your skills for increased job security
- Extending Lua: Making your own libraries.
- 不带头结点的链表大例题
- UVA 1590 IP Networks 简单模拟
- CSDN博客如何转载
- [生存志] 第117节 九章算术均徭赋
- 1.18
- (六)charper3-Extending Your Blog Application之djangoByExample阅读笔记
- spring框架学习(三)junit单元测试
- 关于在android webview上运行html5的开发经验
- [BZOJ2178]圆的面积并(辛普森积分)
- 使用poi分多级sheet生成excel
- vim如何显示行号
- HDU 1166 敌兵布阵(线段树单点更新)
- 多平台轻量级消息推送系统msgd
- pfring 调研+总结