我想在上下文处理器中注册一个变量,但出现的问题是它不起作用并且没有显示任何错误。
View .py:-
def newmessage(request):
getmessagevalue = interview.objects.all()
return {'getmessagevalue': getmessagevalue }
设置.py:-
TEMPLATE_CONTEXT_PROCESSORS = (
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.static",
"django.core.context_processors.media",
"django.core.context_processors.request",
"django.core.context_processors.tz",
"userprofile.views.newmessage"
)
我该怎么办,错误和值不显示。
最佳答案
您向我们展示的代码对我来说看起来不错。您需要确保您的 View (未显示)正在使用请求上下文呈现模板。 See the docs了解更多信息。
将日志记录或打印语句添加到您的上下文处理器,以确保它在您的 View 中运行。也许没有interview
数据库中的对象,因此它返回一个空查询集。
帮助调试的另一个选项是安装 Django debug toolbar .它有一个 Template
面板显示模板上下文处理器的输出。
另外几个建议:
Interview
, 并在您的方法中使用下划线 new_message
和变量 get_message_value
名字。 userprofile.context_processors
而不是在您的 View 模块中。 如果您遵循这些约定,您的代码对于有经验的 python 和 Django 开发人员来说更容易阅读,因此您更有可能得到问题的答案。
关于python - Django 上下文处理器中的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18634231/