有哪些有用的非内置 Django 标签?

发布于 2024-08-06 18:53:00 字数 156 浏览 7 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

羁〃客ぐ 2024-08-13 18:53:00

我开始吧。

http://www.djangosnippets.org/snippets/1350/

智能 {% if % } 模板标签

如果您发现自己需要的不仅仅是 True 测试,那么这个标签适合您。它支持等于、大于和小于运算符。

简单的例子

{% block list-products %}
    {% if products|length > 12 %}
        <!-- Code for pagination -->
    {% endif %}

    <!-- Code for displaying 12 products on the page -->

{% endblock %}

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

{% block list-products %}
    {% if products|length > 12 %}
        <!-- Code for pagination -->
    {% endif %}

    <!-- Code for displaying 12 products on the page -->

{% endblock %}
不离久伴 2024-08-13 18:53:00

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.

誰ツ都不明白 2024-08-13 18:53:00

James Bennet 的 over-the-top-dynamic get_latest 标签

编辑作为对 jpartogi 评论的回应

class GetItemsNode(Node):
    def __init__(self, model, num, by, varname):
        self.num, self.varname = num, varname
        self.model = get_model(*model.split('.'))
        self.by = by

    def render(self, context):
        if hasattr(self.model, 'publicmgr') and not context['user'].is_authenticated():
            context[self.varname] = self.model.publicmgr.all().order_by(self.by)[:self.num]
        else:
            context[self.varname] = self.model._default_manager.all().order_by(self.by)[:self.num]
        return  ''

<div id="news_portlet" class="portlet">
{% get_sorted_items cms.news 5 by -created_on as items %}
{% include 'snippets/dl.html' %}
</div>
<div id="event_portlet" class="portlet">
{% get_sorted_items cms.event 5 by date as items %}
{% include 'snippets/dl.html' %}
</div>

我称之为 get_sorted_items,但它是基于 James 的博客-邮政

James Bennet's over-the-top-dynamic get_latest tag

edit as response to jpartogi's comment

class GetItemsNode(Node):
    def __init__(self, model, num, by, varname):
        self.num, self.varname = num, varname
        self.model = get_model(*model.split('.'))
        self.by = by

    def render(self, context):
        if hasattr(self.model, 'publicmgr') and not context['user'].is_authenticated():
            context[self.varname] = self.model.publicmgr.all().order_by(self.by)[:self.num]
        else:
            context[self.varname] = self.model._default_manager.all().order_by(self.by)[:self.num]
        return  ''

<div id="news_portlet" class="portlet">
{% get_sorted_items cms.news 5 by -created_on as items %}
{% include 'snippets/dl.html' %}
</div>
<div id="event_portlet" class="portlet">
{% get_sorted_items cms.event 5 by date as items %}
{% include 'snippets/dl.html' %}
</div>

I call it get_sorted_items, but it is based on James' blog-post

梦亿 2024-08-13 18:53:00

在这种情况下 {% autopaginate queryset %} (http://code.google.com/p /django-pagination/)很有用。例如:

#views.py
    obj_list = News.objects.filter(status=News.PUBLISHED)
    # do not use len(obj_list) - it's evaluate QuerySet
    obj_count = obj_list.count()

#news_index.html
    {% load pagination_tags %}
    ...
    # do not use {% if obj_list %}
    {% if obj_count %}
        <div class="news">
        <ul>
        {% autopaginate obj_list 10 %}
        {% for item in obj_list %}
            <li><a href="...">{{ item.title }}</a></li>
        {% endfor %}
        </ul>
        </div>
        {% paginate %}
    {% else %}
        Empty list
    {% endif %}

请注意,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:

#views.py
    obj_list = News.objects.filter(status=News.PUBLISHED)
    # do not use len(obj_list) - it's evaluate QuerySet
    obj_count = obj_list.count()

#news_index.html
    {% load pagination_tags %}
    ...
    # do not use {% if obj_list %}
    {% if obj_count %}
        <div class="news">
        <ul>
        {% autopaginate obj_list 10 %}
        {% for item in obj_list %}
            <li><a href="...">{{ item.title }}</a></li>
        {% endfor %}
        </ul>
        </div>
        {% paginate %}
    {% else %}
        Empty list
    {% endif %}

Note, that obj_list must be lazy - read http://docs.djangoproject.com/en/dev/ref/models/querysets/#id1

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文