【问题标题】:Django - template inheritance with includesDjango - 带有包含的模板继承
【发布时间】:2011-01-23 23:35:16
【问题描述】:

我有一个网络应用程序,用户可以在其中拥有个人资料(有点像 facebook),他们可以查看自己的个人资料以及其他人的个人资料。您在自己的个人资料中看到的将是所有内容,但查看您个人资料的其他人可能无法看到其中的所有内容。

为了做到这一点,我有 common-profile.html 和 profile.html 其中 profile.html 包括 common-profile.html 和 common-profile.html 是每个人都可以看到的。因此,如果我想查看自己的个人资料,我会看到 profile.html,但其他人会看到 common-profile.html。

问题是,当我使用模板继承时,这两个模板都继承自某个基本模板,因此模板会被导入两次。

profile.html:

{% extends 'base.html' %}

{% block content %}
{% include 'common-profile.html' %}
...other stuff would go here
{% endblock %}

common-profile.html:

{% extends 'base.html' %}

{% block content %}
<h1>{{c_user.first_name}} {{c_user.last_name}}<h1>
...other stuff would go here
{% endblock %}

这只是个坏主意吗?我应该只有一个配置文件并检查权限/在模板标签中使用一些 if 语句吗?我不想在我的 html 页面中包含太多逻辑,但如果只是一些 if 语句来决定显示什么,也许没关系?

【问题讨论】:

    标签: python django


    【解决方案1】:

    不使用包含,而是将profile.html 扩展为common-profile.html 怎么样?然后在公共配置文件模板中有一个空块,非公共配置文件模板可以添加东西。像这样的:

    common-profile.html:

    {% extends 'base.html' %}
    
    {% block content %}
        <!-- Normal common profile stuff -->
    
        {% block extendedcontent %}{% endblock extendedcontent %}
    {% endblock content %}
    

    profile.html:

    {% extends 'common-profile.html' %}
    
    {% block extendedcontent %}
        <!-- Special profile stuff -->
    {% endblock extendedcontent %}
    

    【讨论】:

    • 是的,这肯定会解决问题。你对拥有两个单独的 html 文件而不是一个只说“如果你有权限看到这个,就在这里”有什么想法
    • 好吧,如果隐私是一个主要问题(这通常是 Facebook 等网站的问题),拥有两个单独的文件可以减少拼写错误导致意外泄露个人信息的可能性应该是私有的。
    猜你喜欢
    • 2010-10-30
    • 1970-01-01
    • 1970-01-01
    • 2018-01-14
    • 2013-01-09
    • 2010-12-23
    • 2021-09-16
    • 1970-01-01
    相关资源
    最近更新 更多