/ context 处的 TypeError 必须是 dict 而不是 RequestContext

我有一个基于 Django 1.9 构建的应用程序,但现在我已将其升级到 2.2。好吧,我遇到了一个奇怪的错误,我无法使用现有的类似解决方案来解决它。在我的例子中,我们从一个不同的文件中获取 templateResponse 然后是内置的


[说 x.py]-


class TemplateResponse(DefaultResponse):

"""A normal response involving data that can be sent to fill in a

template.  Since the template is specific to HTML responses, when

a JSON response is desired, this class is indistinguishable

from DefaultResponse."""


def __init__(self, template, data, status=200):

    self.data = data

    self.template = template

    self.status = status

[基础.py]-


def home(guts): 

    return TemplateResponse(template, {'c_sk': c_nt,

                                   'd_sk_count': d_sk_count} 

回溯错误 -


response = get_response(request) …

▶ Local vars

/home/gaurav/Desktop/a Upgrade Work/a/new_cl/lib/python3.6/site-packages/django/core/handlers/base.py in _get_response

                response = self.process_exception_by_middleware(e, request) …

    ▶ Local vars 


/home/gaurav/Desktop/a Upgrade Work/a/new_cl/lib/python3.6/site-packages/django/core/handlers/base.py in _get_response

                response = wrapped_callback(request, *callback_args, **callback_kwargs) …

    ▶ Local vars

/usr/lib/python3.6/contextlib.py in inner

                return func(*args, **kwds) …

    ▶ Local vars

/home/gaurav/Desktop/a Upgrade Work/a/new_cl/lib/python3.6/site-packages/django/contrib/auth/decorators.py in _wrapped_view

                return view_func(request, *args, **kwargs) …

    ▶ Local vars

/home/gaurav/Desktop/a Upgrade Work/a/main/x.py in g

            response = seed.sprout(context, format) …

    ▶ Local vars

/home/gaurav/Desktop/a Upgrade Work/a/main/x.py in sprout

            return self.sprout_html(context) …

    ▶ Local vars

/home/gaurav/Desktop/a Upgrade Work/a/main/x.py in sprout_html

        body = self.template.render(context) …

    ▶ Local vars


富国沪深
浏览 124回答 2
2回答

慕标琳琳

我通过更改下面提到的代码解决了这个问题 -def home(guts):&nbsp;&nbsp; return TemplateResponse(template.render(mt_context1))&nbsp;但是,这又引入了另一个错误。不确定我是否可以在这里发布这个问题,但由于它似乎与此有关,我在下面提到它,任何对此的专业人士,我真的可以使用一些建议:)追溯:File "/home/gaurav/Desktop/a scratch/onenv/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner34.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;response = get_response(request)File "/home/gaurav/Desktop/a scratch/onenv/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response115.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;response = self.process_exception_by_middleware(e, request)File "/home/gaurav/Desktop/a scratch/onenv/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response113.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;response = wrapped_callback(request, *callback_args, **callback_kwargs)File "/usr/lib/python3.6/contextlib.py" in inner52.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return func(*args, **kwds)File "/home/gaurav/Desktop/a scratch/onenv/lib/python3.6/site-packages/django/contrib/auth/decorators.py" in _wrapped_view21.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return view_func(request, *args, **kwargs)File "/home/gaurav/Desktop/a scratch/a/main/wrapper.py" in g339.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;seed = d[request.method](guts, *args, **kwargs)File "/home/gaurav/Desktop/a scratch/a/main/wrapper.py" in <lambda>367.&nbsp; &nbsp; &nbsp;return dispatch_on_method(f, {"GET": lambda guts, *args, **kwargs: f(guts, *args, **kwargs)})File "/home/gaurav/Desktop/a scratch/a/main/views/base.py" in home104.&nbsp; &nbsp; &nbsp;return TemplateResponse(template.render(mt_context1))异常类型:TypeError at / 异常值:init()缺少1个必需的位置参数:'data'

largeQ

您应该将传入请求传递给 TemplateResponsedef&nbsp;home(request): &nbsp;&nbsp;&nbsp;&nbsp;response&nbsp;=&nbsp;TemplateResponse(request,&nbsp;'mytemplate.html',&nbsp;{'c_sk':&nbsp;c_nt,'d_sk_count':&nbsp;d_sk_count}&nbsp;) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;response
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python