python - Django 1.11 上下文处理器错误 : TypeError: context must be a dict rather than RequestContext'

标签 python django requestcontext

我不明白为什么我遇到 Django 1.11 和 RenderContext 问题。我真的需要帮助。这是我在 1.11 的官方文档中一直使用的代码:

from django.http import HttpResponse
from django.template import RequestContext, Template
from django.template import loader

def ip_address_processor(request):
    return {'ip_address': request.META['REMOTE_ADDR']}


def view_2(request):
    template = loader.get_template('template2.html')
    ctx = RequestContext(request, {
        'title': 'Your IP Address',
    }, [ip_address_processor])
    return HttpResponse(template.render(ctx))

还有我的简单模板:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
Test
{{ title }}: {{ ip_address }}
</body>
</html>

这会导致以下错误:

Internal Server Error: /view2/
Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\django\core\handlers\exception.py", line 41, in inner
    response = get_response(request)
  File "C:\Python27\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Python27\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\null\PycharmProjects\project1\project1\views.py", line 48, in view_2
    return template.render(c)
  File "C:\Python27\lib\site-packages\django\template\backends\django.py", line 64, in render
    context = make_context(context, request, autoescape=self.backend.engine.autoescape)
  File "C:\Python27\lib\site-packages\django\template\context.py", line 287, in make_context
    raise TypeError('context must be a dict rather than %s.' % context.__class__.__name__)
TypeError: context must be a dict rather than RequestContext.
[07/Aug/2017 23:52:49] "GET /view2/ HTTP/1.1" 500 72701

这对我来说很奇怪,因为下面的代码有效:

from django.http import HttpResponse
from django.template import RequestContext, Template
from django.template import loader

def ip_address_processor(request):
    return {'ip_address': request.META['REMOTE_ADDR']}


def view_2(request):

    template = Template('{{ title }}: {{ ip_address }}')
    ctx = RequestContext(request, {
        'title': 'Your IP Address',
    }, [ip_address_processor])
    return HttpResponse(template.render(ctx))

通过覆盖 Template 对模板进行硬编码工作正常,但使用 django.template.loader.get_loader 导入它却不行???我真的在这里不知所措。

我做错了什么?模板正在做完全相同的事情。这真的让我从 1.11 回来了。它曾经是你可以在 Django 1.8 中传递一个 context_instance 并且它只是工作。我似乎无法在 1.11 中运行任何上下文处理器,即使使用 docs.djangoproject.com 上记录的示例也是如此。只有当我调用 Template 并通过硬编码传递我的模板时,它才会起作用。

最佳答案

作为The_Cthulhu_Kid said in a comment ,Django 1.11 弃用了在非字典上下文中的传递:

https://docs.djangoproject.com/en/1.11/releases/1.11/#django-template-backends-django-template-render-prohibits-non-dict-context

我想出了一个简单的例子,如果有人在想你会如何在 1.11 中做内容处理器的话

我将上面的示例代码更改为:

def ip_address_processor(request):
    return {'ip_address': request.META['REMOTE_ADDR'], 'ua': request.META['HTTP_USER_AGENT']}


def view_2(request):
    template = loader.get_template('template2.html')
    proc_ex = ip_address_processor(request)
    context = {'ua': proc_ex.get('ua'),
               'ip_address': proc_ex.get('ip_address'),
               'title': 'TEST'}
    return HttpResponse(template.render(context))

和模板:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
{{ title }}: {{ ip_address }}
User-Agent: {{ ua }}
</body>
</html>

您也可以这样做,以避免必须将键与 ip_address_processor 函数对齐:

def ip_address_processor(request):
    return {'ip_address': request.META['REMOTE_ADDR'], 'ua': request.META['HTTP_USER_AGENT']}


def view_2(request):
    template = loader.get_template('template2.html')
    proc_ex = ip_address_processor(request)
    proc_ex.update({'title': 'test2'})
    return HttpResponse(template.render(proc_ex))

看起来这里的关键是只要给它一个字典,它就会很高兴。

关于python - Django 1.11 上下文处理器错误 : TypeError: context must be a dict rather than RequestContext',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45561500/

相关文章:

python - 如何将数组从ABAB更改为AABB

django - 将 django 应用程序部署到 amazon beanstalk 时出现数据库问题

django - AWS S3和Django返回 "An error occurred (AccessDenied) when calling the PutObject operation"

python - 如何为现有项目生成 asgi.py?

django - 为什么我必须在所有响应中传递 RequestContext?

jsf - Primefaces RequestContext scrollTo 不起作用

python - 不创建索引的 SQL 优化

python - 使用PyQt5嵌入动态条形图

python - 如何在 cypher 程序中返回字符数组(python3)

jsf - Primefaces JSF 从 RequestContext.getCurrentInstance() 返回 null