为什么django实际加载模板内容?

时间:2014-06-28 07:58:29

标签: django django-templates

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

> {% 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 #}`

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

2 个答案:

答案 0 :(得分:0)

您可能无法渲染模板

尝试类似:

from django.shortcuts import render

def myview(request):

    return render(request, 'path/template.html', {})

答案 1 :(得分:0)

在呈现模板时看到原始代码的原因是语句必须在一行上,包括标记和注释。

对于多行注释,您可以使用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

更高级的逻辑,就像你在这里尝试做的那样,应该在view函数中完成,并传递给模板的上下文。