传递要在django模板中显示的变量

传递要在django模板中显示的变量,django,django-templates,Django,Django Templates,嗨,我是django的新手,有件事我搞不懂。如何在不同的文件中显示变量集 choices.py: testvar= 'This is a test variable' views.py from .choices import testvar template.html {{testvar}} 是这样吗?导入似乎工作正常,但字符串未显示。 谢谢。不用了 正如本教程所解释的,您需要将其作为上下文条目传递给模板对象的render()方法。您的视图必须类似于: from django.short

嗨,我是django的新手,有件事我搞不懂。如何在不同的文件中显示变量集

choices.py:

testvar= 'This is a test variable'
views.py

from .choices import testvar
template.html

{{testvar}}
是这样吗?导入似乎工作正常,但字符串未显示。 谢谢。不用了


正如本教程所解释的,您需要将其作为上下文条目传递给模板对象的
render()
方法。

您的视图必须类似于:

from django.shortcuts import render

def index(request):
    testvar = 'value'
    return render(request, 'template.html', {'testvar': testvar})
您传递包含您的值的字典