【问题标题】:why django load actually the template contents?为什么 django 实际加载模板内容?
【发布时间】:2014-08-19 07:59:54
【问题描述】:

运行后我的浏览器显示如下:**

> {% set enabled_scopes_class = 'scopes-' +
> '%s'|format(settings.ALL_SCOPE_ENABLED) + '-' +
> '%s'|format(settings.UNANSWERED_SCOPE_ENABLED) + '-' +
> '%s'|format((request.user.is_authenticated() and
> settings.FOLLOWED_SCOPE_ENABLED)) %} {# Some or all contents of this
> div may be dropped over the search bar via negative margins, to make
> sure that the search bar can occupy 100% of the content width. Search
> bar may have padding on the left and right to accomodate the buttons.
> `#}{# three buttons below are in the opposite order because they are
> floated at the right #}`

请指导我实现目标的正确方法。

【问题讨论】:

    标签: django django-templates


    【解决方案1】:

    您在渲染模板时看到原始代码的原因是语句必须在一行中,包括标记和 cmets。

    对于多行 cmets,您可以使用 comment 标签:

    {% comment %}
    Some or all contents of this
    div may be dropped over the search bar via negative margins, to make
    sure that the search bar can occupy 100% of the content width. Search
    bar may have padding on the left and right to accomodate the buttons.
    three buttons below are in the opposite order because they are
    floated at the right
    {% endcomment %}
    

    至于您的set 声明:我不知道任何set 声明(它是第三方标签吗?),但模板语言在设计上不如Python 代码强大。不允许使用括号对语句进行分组,并且不能像在 python 中那样调用带参数的函数。您也不能将值与+format 连接起来不是定义的模板过滤器。我建议您阅读模板in the documentation

    更高级的逻辑,比如你在这里尝试做的,应该在视图函数中完成,并传递给模板的上下文。

    【讨论】:

    • 问题不在于多行 cmets !当我删除所有评论时,它再次显示模板内容!这是我在父模板或此模板中调用的问题吗?这是基本模板的一部分(浏览器显示 secondary_header.html): {% include "custom_header.html" %} {% if settings.CUSTOM_HEADER != '' %}
      {{settings .CUSTOM_HEADER}}
      {% endif %} {% include "widgets/header.html" %} {# 徽标、用户工具导航和元导航 #} {% include "widgets/secondary_header.html" %}
    • @bluebird7 那么set 标签呢?完整的标签(从{%%})是否在一行中?
    • 是的,我将块写在 1 行中,它完成了!多谢 !那个浏览器之后的机器人没有显示任何想法......我不知道尝试找到问题!
    【解决方案2】:

    你可能没有渲染你的模板

    尝试类似:

    from django.shortcuts import render
    
    def myview(request):
    
        return render(request, 'path/template.html', {})
    

    【讨论】:

    • 不,我这样做........!我认为没有渲染浏览器不会引发任何事情!!!
    猜你喜欢
    • 2018-09-24
    • 1970-01-01
    • 2014-01-21
    • 1970-01-01
    • 1970-01-01
    • 2010-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多