【问题标题】:Python template include a page if conditionPython 模板包含一个页面 if 条件
【发布时间】:2012-08-08 10:27:47
【问题描述】:

我正在使用 Python 和 Google 应用引擎开发一个小型应用。我使用样板(https://github.com/coto/gae-boilerplate)作为前端,它遵循 gae 方向和 python 模板,所以没有什么比普通的东西不同的了。

现在,我想要的是这个。 当用户登录时,如果没有填写姓名和姓氏字段,我希望在主页中进行个人资料编辑。

用于编辑配置文件的页面是一个模板(它扩展了 base.html),名为 edit_profile.html,效果很好。 主页也是一个名为 home.html 的模板(扩展 base.html)。

现在,我可以在 home.html 中包含 edit_profile.html 吗?我该怎么做?

这就是我所拥有的,我不知道该放什么来代替????我试过了

 {% block edit_profile.html %}  {% endblock %}

但不起作用

{% if user_info.name and user_info.last_name %}
        ..
        {% else %}
           ????
        {% endif %}

谢谢。

【问题讨论】:

    标签: python google-app-engine templates boilerplate


    【解决方案1】:

    所以你只想包含给定模板的一些块。有两种解决方案:

    1) 为个人资料编辑表单创建模板并将其包含在edit_profile.html 中。然后将其也包含到home.htmlif 条件分支中:

    profile_form.html:

    <form action="{% url some-action %}">
    {{ form }}
    <input type="submit" value="save"/>
    </form
    

    profile_edit.html

    {% extends "base.html" %}
    
    {% block main %}
    {% include "profile_form.html" %}
    {% endblock %}
    

    home.html

    {% if user_info.name and user_info.last_name %}
    {% include "profile_form.html" %}
    {% endif %}
    

    2) 扩展模板使用变量:

    profile_form.html

    {% extend BASE_TEMPLATE %}
    

    并根据需要将其设置为具有不同值的上下文:

    在 home.html 中(假设included_form.html 是一些基本模板)

    {% if user_info.name and user_info.last_name %}
    {% with "included_form.html" as BASE_TEMPLATE %}
       {% include "edit_profile.html" %}
    {% endwith %}
    {% endif %}
    

    如果您想将表单显示为独立页面,请将 BASE_TEMPLATE 设置为 base.html

    【讨论】:

    • 嗯。似乎我必须重写样板提供的模板。第二种方法不起作用,也许我做错了什么。无论如何,为了使示例更清晰,我想在我的页面中包含的内容是此模板github.com/coto/gae-boilerplate/blob/master/templates/… 的内容块内的内容,我可以对模板说包含该文件的内容块吗?
    • 我可以告诉模板包含该文件的内容块吗? - 没有
    猜你喜欢
    • 2016-03-22
    • 1970-01-01
    • 2014-08-26
    • 2012-08-15
    • 2021-04-06
    • 2021-03-01
    • 2021-06-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多