Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django上下文处理器中的变量_Python_Django_Function_Error Handling - Fatal编程技术网

Python Django上下文处理器中的变量

Python Django上下文处理器中的变量,python,django,function,error-handling,Python,Django,Function,Error Handling,我想在上下文处理器中注册一个变量,但出现的问题是它不工作,也不显示任何错误 views.py:- def newmessage(request): getmessagevalue = interview.objects.all() return {'getmessagevalue': getmessagevalue } settings.py:- TEMPLATE_CONTEXT_PROCESSORS = ( "django.contrib.auth.context_p

我想在上下文处理器中注册一个变量,但出现的问题是它不工作,也不显示任何错误

views.py:-

def newmessage(request):
    getmessagevalue = interview.objects.all()
    return {'getmessagevalue': getmessagevalue }
settings.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"

)

我能做什么,错误和值不会显示。

您向我们显示的代码在我看来正常。您需要确保您的视图(尚未显示)正在使用请求上下文呈现模板。了解更多信息

向上下文处理器添加日志记录或打印语句,以确保它在视图中运行。可能数据库中没有
interview
对象,因此它返回一个空查询集

帮助调试的另一个选项是安装。它有一个
模板
面板,显示模板上下文处理器的输出

还有几点建议:

  • 命名您的模型
    Interview
    ,并在方法
    new\u message
    和变量
    get\u message\u value
    名称中使用下划线
  • 将上下文处理器放在单独的模块
    userprofile.context\u处理器中,而不是放在视图模块中

如果遵循这些约定,有经验的python和Django开发人员将更容易阅读您的代码,因此您更有可能获得问题的答案。

如何在html(模板)代码中访问这些值?