为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

富国沪深 2022-05-24 16:09:43
我有一个基于 Django 1.9 构建的应用程序,但现在我已将其升级到 2.2。好吧,我遇到了一个奇怪的错误,我无法使用现有的类似解决方案来解决它。在我的例子中,我们从一个不同的文件中获取 templateResponse 然后是内置的[说 x.py]-class TemplateResponse(DefaultResponse):"""A normal response involving data that can be sent to fill in atemplate.  Since the template is specific to HTML responses, whena JSON response is desired, this class is indistinguishablefrom 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
查看完整描述

2 回答

?
慕标琳琳

TA贡献1830条经验 获得超9个赞

我通过更改下面提到的代码解决了这个问题 -


def home(guts): 

  return TemplateResponse(template.render(mt_context1)) 

但是,这又引入了另一个错误。


不确定我是否可以在这里发布这个问题,但由于它似乎与此有关,我在下面提到它,任何对此的专业人士,我真的可以使用一些建议:)


追溯:


File "/home/gaurav/Desktop/a scratch/onenv/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner

34.             response = get_response(request)


File "/home/gaurav/Desktop/a scratch/onenv/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response

115.                 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_response

113.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)


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

52.                 return func(*args, **kwds)


File "/home/gaurav/Desktop/a scratch/onenv/lib/python3.6/site-packages/django/contrib/auth/decorators.py" in _wrapped_view

21.                 return view_func(request, *args, **kwargs)


File "/home/gaurav/Desktop/a scratch/a/main/wrapper.py" in g

339.                 seed = d[request.method](guts, *args, **kwargs)


File "/home/gaurav/Desktop/a scratch/a/main/wrapper.py" in <lambda>

367.     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 home

104.     return TemplateResponse(template.render(mt_context1))

异常类型:TypeError at / 异常值:init()缺少1个必需的位置参数:'data'


查看完整回答
反对 回复 2022-05-24
?
largeQ

TA贡献2039条经验 获得超7个赞

您应该将传入请求传递给 TemplateResponse

def home(request):
    response = TemplateResponse(request, 'mytemplate.html', {'c_sk': c_nt,'d_sk_count': d_sk_count} )
        return response


查看完整回答
反对 回复 2022-05-24
  • 2 回答
  • 0 关注
  • 123 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信