【问题标题】:django template conditional extenddjango模板条件扩展
【发布时间】:2021-10-08 17:35:26
【问题描述】:

我有一个非常具体的问题。我正在使用maintenance mode 来获得内置的 503 视图。我可以以任何我想要的方式自定义模板。我得到了它的工作,但是我希望模板在 request.path 为 /admin 时扩展管理基本模板。如果不是,它应该扩展正常的基本模板。

我试过了:

{% if '/beheerpaneel' in request.path %}
    {% extends "admin/index.html" %}
{% else %}
    {% extends "base.html" %}
{% endif %}

但这给了我一个模板语法错误,因为扩展标签必须是第一个。经过一番谷歌搜索后,我似乎需要在视图中处理这个问题。但没有视图,它是内置的。有人知道解决这个问题的方法吗?将来我也想用 404 和 500 页等来做这个。

【问题讨论】:

    标签: django django-views django-templates syntax-error


    【解决方案1】:

    你应该在上下文中这样做。因此,在您看来,您可以使用:

    def my_view(request):
        if '/beheerpaneel' in request.path:
            template_base = 'admin/index.html'
        else:
            template_base = 'base.html'
        # …
        return render(request, 'your-template.html', {'template_base': template_base})

    然后使用:

    {% extends template_base %}
    <!-- ... -->

    【讨论】:

    • 我没有视图可以使用,它是内置的
    猜你喜欢
    • 2015-03-01
    • 2016-06-11
    • 2010-11-27
    • 1970-01-01
    • 2017-04-24
    • 2017-04-10
    • 2012-01-10
    • 2017-08-30
    相关资源
    最近更新 更多