我有一个Web应用程序,用户可以在其中拥有个人资料(有点像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语句来决定要显示什么,也许可以吗?

最佳答案

profile.html扩展为common-profile.html而不是使用include呢?然后,在通用配置文件模板中只有一个空白块,非通用配置文件模板可以向其中添加内容。像这样:

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

关于python - Django-具有include的模板继承,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4777317/

10-14 18:15
查看更多