【发布时间】:2018-08-09 12:42:00
【问题描述】:
在advice here 之后,我可以访问模板中的 allowed_contributors 变量并且可以将其打印出来,但在任何类型的 if-else 语句中使用它都不起作用。它不会给我一个 500 错误,但它就像是空的一样。
我从模板标签加载的文件:
from django import template
from django.conf import settings
register = template.Library()
@register.simple_tag
def allowed_contributors():
return getattr(settings, "ALLOWED_CONTRIBUTORS", "")
这是我在模板中添加的内容(顶部没有显示“加载”命令,但我想这一定是有效的)。
<div class="container">
<h1>Create new project</h1>
<p> {% allowed_contributors %} </p>
{% if "true" in allowed_contributors %}
<p>"true" found in allowed_contributors!</p>
{% endif %}
{% if "false" in allowed_contributors %}
<p>"false" found in allowed_contributors!</p>
{% endif %}
</div>
HTML 输出如下所示:
<div class="container">
<h1>Create new project</h1>
<p> ('auth', 'false') </p>
</div>
我已尝试多次输出 allowed_contributors 以防第一次被消耗,但似乎没有任何区别。
当我将它用作 if 语句的条件时,是否需要以不同的方式引用它?
如果有帮助,我正在使用 Django 1.8
编辑:提供的任何明智的答案都不适合我,可能是由于我不知道该项目的其他一些配置。我已经通过使用稍微复杂的context_processor solution 来解决它。
【问题讨论】:
标签: django django-templates django-settings