本文介绍了迁移到Django 1.11-“上下文必须是字典而不是上下文。”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从1.10迁移到1.11.15之后,我就开始出现此错误。

I started getting this error right after migration to 1.11.15 from 1.10.

以下是视图代码:

def signin(request):
    form = forms.LoginForm()
    form_reset = forms.PasswordResetForm()

    if request.method == "POST":
        form = forms.LoginForm(request.POST)
        if form.is_valid():
            login(request, form.user)
            messages.success(request, "You are now signed in")
            return redirect(reverse('landscape'))

    d = {
        'form': form,
        'form_reset': form_reset
    }

    return render(request, "signin.html", d)

return语句是异常跟踪中的语句。如您所见,我正在将 dict(而不是上下文)传递给渲染快捷方式函数。

the "return" statement is the one in the exception trace. As you can see I am passing "dict" (not Context) into the render shortcut function.

我经过了整个堆栈跟踪,可以看到字典确实被更改为Django lib(/usr/local/lib/python2.7/dist-packages/django/template/backends/django.py)内的Context对象:

I went thru the whole stack trace and I can see that the dictionary indeed gets changed into a Context object inside the django lib (/usr/local/lib/python2.7/dist-packages/django/template/backends/django.py):

class Template(object):

    def __init__(self, template, backend):
        self.template = template
        self.backend = backend

    @property
    def origin(self):
        return self.template.origin

    def render(self, context=None, request=None):
        context = make_context(context, request, autoescape=self.backend.engine.autoescape)
        try:
            return self.template.render(context)
        except TemplateDoesNotExist as exc:
            reraise(exc, self.backend)

和类型检查进一步向下射击。

And the type check fires further down.

任何知道我的设置有什么问题吗?

Any idea what's wrong with my setup?

全屏查看的requirements.txt:

the requirements.txt for full view:

Django==1.11.15
http-parser==0.8.1
httpie==0.8.0
httplib2==0.8
dj-database-url==0.3.0
dj-static==0.0.6
gunicorn==19.1.1
psycopg2==2.7.5
static==0.4
wsgiref==0.1.2
websocket-client==0.32.0
Pillow==2.8.1
django-storages==1.5.2
boto==2.38.0
twitter==1.17.1
rollbar==0.13.11
django-cors-headers==1.1.0
django-annoying==0.10.3
django-letsencrypt==2.0.0
django-compressor==2.1.1
braintree==3.38.0
validate-email==1.3
pydns==2.3.6
asgi-redis==1.4.3
asgiref==1.1.2
channels==1.1.8
django-redis-cache==1.7.1
whitenoise==3.3.1
daphne==1.4.2
django-js-reverse==0.8.1
crypto==1.4.1
cryptography==1.8.1
pycrypto==2.6.1


推荐答案

通过追溯进行挖掘。我意识到跟踪通过所提到的库代码运行了两次。 (一旦将dict更改为Context,并且在上下文已为Context的情况下在第二次运行时中断)

After a lot of digging thru the traceback. I realized the trace is running thru mentioned library code twice. (Once changing dict into Context, and breaking on the second run when context is already Context)

我的模板包含:

{{form|bootstrap}}

来自引导程序表格包。

From bootstrap form package. Removing the line fixed the problem.

我将假定软件包已过期并重写代码。

I am going to assume the package is out of date and rewrite the code.

这篇关于迁移到Django 1.11-“上下文必须是字典而不是上下文。”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 15:46