Django 1.11.17 TypeError: 上下文必须是一个字典而不是Context,除了它是一个字典

2024-05-19 10:53:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我最近从django1.9改为1.11.17,有一件事让我很困扰。有一个错误说明

TypeError at /somepath
context must be a dict rather than Context

抛出它的线是:

^{pr2}$

有很多答案,所以当人们使用RequestContext或Context而不是dict for context并切换到dict来解决他们的问题。但不是为了我。在这里,我很确定我的context实际上是一个dict。如果我把它改成:

return render(request=request, template_name="mytemplate.html", context={})

错误消失了,但很明显会导致以后的另一个错误。你们知道我做错什么了吗?在

编辑: 我的进口:

from django.shortcuts import render, render_to_response
from django.template.context import RequestContext, Context

我尝试过bot render和{},效果相似。使用Context或RequestContext也会产生类似的错误。在

编辑2:更多代码供参考

from django.http import (
    HttpResponseRedirect,
    HttpResponseBadRequest,
)
from django.shortcuts import render, render_to_response
from django.template import RequestContext, Context
from django.utils.html import escape

# some more imports, but from local files, not django

def update_my_template(request):
    user = request.user
    # preform some checks for user
    ...

    if request.method == "GET":
        updateType = request.GET.get("id")
        if updateType:
            form = None
            if updateType == "something":
                form = SomeForm(user)
            if updateType == "something else":
                form = DifferentForm()
            if form is None:
                return HttpResponseRedirect("/somepage")

            # This was the code that worked in 1.9
            rctx = RequestContext(
                request, {"form": form, "update": updateType}
            )
            return render_to_response("mytemplate.html", rctx)



    # some different cases, but the error is thrown already
...

这两项工作都没有:

dictctx = {"form": form, "update": updateType}
return render(request=request, template_name="mytemplate.html", dictctx)

一。在

ctx = Context({"form": form, "update": updateType})
return render(request=request, template_name="mytemplate.html", ctx)

一。在

ctx = Context({"form": form, "update": updateType})
return render(request=request, template_name="mytemplate.html", ctx.flatten())

一。在

rctx = RequestContext(request, {"form": form, "update": updateType})
return render_to_response("mytemplate.html", rctx.flatten())

Tags: djangofromimportformreturnrequesthtmlcontext
3条回答

始终传递参数中的变量/值。但你同时给予两者。试试这个,。。。在

return render(request=request, template_name="mytemplate.html", {"form": form, "update": updateType})

或者

context={"form": form, "update": updateType} return render(request=request, template_name="mytemplate.html",context)

render逻辑不同,这取决于传递给render的内容:

def render(self, context):
        "Display stage   can be called many times"
        with context.render_context.push_state(self):
            if context.template is None:
                with context.bind_template(self):
                    context.template_name = self.name
                    return self._render(context)
            else:
                return self._render(context)

看起来您可以将参数template_name更改为name,但是您的对象没有{}值,这就是为什么创建和使用Context的实例会更好

https://docs.djangoproject.com/en/1.11/_modules/django/template/base/#Template.render

文档显示传递Context的实际实例,因此我建议您在代码中这样做,而不是仅仅传递dict:

^{pr2}$

因此,修复代码的最简单方法如下:

from django.template import Context
...
return render(request=request, template_name="mytemplate.html", context=Context({"form": form, "update": updateType}))

好的,在进行了更多的挖掘之后(在“未解决的”问题中),我找到了this gem。是的,这就是我问题的解决方案。基本上我在我的mytemplate.html中有一行{{form|bootstrap}},这是导致这个问题的原因。在

更好的是,将django-bootstrap-form更新到3.4版,这样我就可以保留{{form|bootstrap}}并消除错误。在

相关问题 更多 >

    热门问题