“上下文必须是字典而不是类型。”无法解决

时间:2019-08-24 09:23:01

标签: django

我想将获得的URL参数用于过滤器,但是无法解决该错误。 我知道原因是“上下文['group'] =归属.objects.get(用户=用户).group”,但我不知道该怎么办。 已获取“用户名”。

#error
context must be a dict rather than type.
#view
def get_context_data(self, **kwargs):
    context = super().get_context_data(**kwargs)
    user = self.kwargs.get('username')
    print(user)
    try:
      context['group'] = belong.objects.get(user=user).group
    except belong.DoesNotExist:
      pass
    except:
      return HttpResponseBadRequest

    try:
      context['count'] = URC.objects.filter(user=user).count()
    except URC.DoesNotExist:
      pass
    except:
      return HttpResponseBadRequest
    context['username'] = user
    return context

后记

Traceback (most recent call last):
  File "/Users/t.a/anaconda3/envs/person/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/Users/t.a/anaconda3/envs/person/lib/python3.7/site-packages/django/core/handlers/base.py", line 145, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Users/t.a/anaconda3/envs/person/lib/python3.7/site-packages/django/core/handlers/base.py", line 143, in _get_response
    response = response.render()
  File "/Users/t.a/anaconda3/envs/person/lib/python3.7/site-packages/django/template/response.py", line 106, in render
    self.content = self.rendered_content
  File "/Users/t.a/anaconda3/envs/person/lib/python3.7/site-packages/django/template/response.py", line 83, in rendered_content
    content = template.render(context, self._request)
  File "/Users/t.a/anaconda3/envs/person/lib/python3.7/site-packages/django/template/backends/django.py", line 59, in render
    context = make_context(context, request, autoescape=self.backend.engine.autoescape)
  File "/Users/t.a/anaconda3/envs/person/lib/python3.7/site-packages/django/template/context.py", line 270, in make_context
    raise TypeError('context must be a dict rather than %s.' % context.__class__.__name__)
TypeError: context must be a dict rather than type.

0 个答案:

没有答案