Kub*_*bas 4 django global-variables django-templates
我的问题是:我应该将模板上下文处理器用于类别列表等全局变量吗?
我有globs.py
from news.models import Category
def globs(request):
cats = Category.objects.all()
return {'cats': cats}
Run Code Online (Sandbox Code Playgroud)
并在settings.py中
TEMPLATE_CONTEXT_PROCESSORS = ("django.core.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"globs.globs",)
Run Code Online (Sandbox Code Playgroud)
当我在模板'cats'中使用它在开发人员服务器上工作正常.
在我的托管上我有一个问题:导入请求处理器模块globs时出错:"没有名为globs的模块"
我可以为全局变量使用其他东西吗?
globs.py需要位于托管服务器上的可导入路径中.您可以将其移动到新闻目录并在TEMPLATE_CONTEXT_PROCESSORS中使用"news.globs.globs".