Django if/else标签

-1 python django templates if-statement

我不知道为什么我收到"结果是假的"代码有什么问题?

x = True
t = Template("""
    {% if x %}
        The result is True.
    {% else %}
        The result is False.
    {% endif %}
    """)
c = Context()
html = t.render(c)
return HttpResponse(html)
Run Code Online (Sandbox Code Playgroud)

问候

mip*_*adi 5

你没有x在上下文中包括:

c = Context()
Run Code Online (Sandbox Code Playgroud)

你应该做:

c = Context({'x': x})
Run Code Online (Sandbox Code Playgroud)