模板包含和django views / urls。他们如何(应该/应该)工作?

时间:2012-04-12 19:48:31

标签: python django templates include

我有一个迷你盒子,在悬停时弹出以显示个人资料信息(不断被隐藏)。它的工作原因是模板包括:

{% for i in leftbar_network|slice:":12" %}
     {% include "includes/mini_profile.html" %} 
{% endfor %}

但我想添加一些条件并提取其他信息......例如:检查是否可以发送消息。拉特定的配置文件朋友计数.. yadda yadda。我有一个网址和视图应该工作。但似乎他们完全被忽视了。

模板:

<div class="mini-profile">
    <div class="mini-profile-top">
        <a href="/profile/{{i.get_type|lower}}/{{ i.user.username }}/" data-title="{{ i.user.get_full_name }}" data-content="{{i.get_type}}">
            <img class="img-frame" width="90" height="90" src="{% if i.avatar %}{% thumbnail i.avatar 120x120 crop %}{% else %}{{ DEFAULT_AVATAR }}{% endif %}" alt="{{ i.user.get_full_name }}" />
        </a>
        <a href="/profile/{{i.get_type|lower}}/{{ i.user.username }}/" data-title="{{ i.user.get_full_name }}" data-content="{{i.get_type}}">{{ i.user.get_full_name }}</a>
        <div>{{ i.get_type }}</div>
    </div>
    <div class="mini-profile-bottom">
        {% if can_message %}
        <form method="GET" action="/messages/compose/{{ i.get_type|lower }}/{{ i.user.username }}/">
            <button class="btn btn-margin" type="submit">
                <i class="icon-envelope"></i> Message
            </button>
        </form>
        {% else %}
        <button class="btn btn-margin tooltip-bottom disabled" title="You need to be connected to message {{profile.user.get_full_name}}" type="submit">
            <i class="icon-envelope"></i> Message
        </button>
        {% endif %}
    </div>
</div>

有没有办法处理{%include%}并生成一个视图?或者我以错误的方式解决了这个问题?如果是这样,我应该如何生成一个带有视图和网址的迷你小盒子?

示例:将鼠标悬停在用户上时进行Google聊天。

提前感谢您的建议。

1 个答案:

答案 0 :(得分:3)

  

我想添加一些条件并提取其他信息......   实例:检查是否可以发送消息。拉那个特定的配置文件   朋友数

听起来您正在为inclusion tag寻找更具体的模板标签。

因此,您可以将(python / view)逻辑放在标记中,并将适合的html呈现给您的include。