有哪些有用的非内置 Django 标签?
我对 Django 比较陌生,我正在尝试为未来的项目构建我的工具箱。在我的上一个项目中,当内置模板标签不能完全满足我的需要时,我会将模板弄得一团糟,以将功能塞进鞋拔子中。后来我找到了一个模板标签,它可以节省我的时间和丑陋的代码。
那么 Django 中没有内置哪些有用的模板标签呢?
I'm relatively new to Django and I'm trying to build up my toolbox for future projects. In my last project, when a built-in template tag didn't do quite what I needed, I would make a mangled mess of the template to shoe-horn in the feature. I later would find a template tag that would have saved me time and ugly code.
So what are some useful template tags that doesn't come built into Django?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我开始吧。
http://www.djangosnippets.org/snippets/1350/
智能 {% if % } 模板标签
如果您发现自己需要的不仅仅是 True 测试,那么这个标签适合您。它支持等于、大于和小于运算符。
简单的例子
I'll start.
http://www.djangosnippets.org/snippets/1350/
Smart {% if %} template tag
If you've ever found yourself needing more than a test for True, this tag is for you. It supports equality, greater than, and less than operators.
Simple Example
smart-if。如果 x > 则允许正常
y
在模板中构造,等等。更好的
if
标签现在是 Django 1.2 的一部分(请参阅 发行说明),计划于 3 月发布2010 年 9 日。smart-if. Allows normal
if x > y
constructs in templates, among other things.A better
if
tag is now part of Django 1.2 (see the release notes), which is scheduled for release on March 9th 2010.James Bennet 的 over-the-top-dynamic
get_latest
标签编辑作为对 jpartogi 评论的回应
我称之为
get_sorted_items
,但它是基于 James 的博客-邮政James Bennet's over-the-top-dynamic
get_latest
tagedit as response to jpartogi's comment
I call it
get_sorted_items
, but it is based on James' blog-post在这种情况下 {% autopaginate queryset %} (http://code.google.com/p /django-pagination/)很有用。例如:
请注意,obj_list必须是惰性的 - 阅读http://docs.djangoproject.com/en/dev/ref/models/querysets/#id1
In come case {% autopaginate queryset %} (http://code.google.com/p/django-pagination/) is useful. For example:
Note, that obj_list must be lazy - read http://docs.djangoproject.com/en/dev/ref/models/querysets/#id1