Python模板包含一个页面if条件

时间:2012-08-08 10:27:47

标签: python google-app-engine templates boilerplate

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

现在,我想拥有的就是这个。 当用户登录时,如果未填写姓名和姓氏字段,我希望在主页中进行配置文件编辑。

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

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

这就是我,我不知道该放什么而不是????我试过

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

但不起作用

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

感谢。

1 个答案:

答案 0 :(得分:2)

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

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 %}

如果您希望将show form作为独立页面,请将BASE_TEMPLATE设置为base.html