在我的视图渲染中,我返回以下上下文字典:
company = get_object_or_404(Company, slug=slug)
context = {
'company':company,
}
return render(request, 'company-detail.html', context)
我在模板标签中使用它来检查所有权:
@register.filter(name='is_owner')
def is_owner(user, company):
if user.customer.company.id == company.id:
return True
else:
return False
我尝试在模板中看到标记的输出:
{{user|is_owner:customer.id}}
我收到以下错误:
Internal Server Error: /companies/my-company/ Traceback (most recent call last): File "/django/template/base.py", line 835, in
_resolve_lookup
current = current[bit] File "/django/template/context.py", line 83, in __getitem__
raise KeyError(key) KeyError: 'customer'
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File "/django/template/base.py", line 841, in _resolve_lookup
if isinstance(current, BaseContext) and getattr(type(current), bit): AttributeError: type object 'RequestContext' has no attribute 'customer'
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File "/django/template/base.py", line 849, in _resolve_lookup
current = current[int(bit)] ValueError: invalid literal for int() with base 10: 'customer'
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File "/django/core/handlers/exception.py", line 35, in inner
response = get_response(request) File "/django/core/handlers/base.py", line 128, in _get_response
response = self.process_exception_by_middleware(e, request) File "/django/core/handlers/base.py", line 126, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/companies/views.py", line 250, in company_detail_view
return render(request, 'companies/company-detail.html', context) File "/django/shortcuts.py", line 36, in render
content = loader.render_to_string(template_name, context, request, using=using) File "/django/template/loader.py", line 62, in render_to_string
return template.render(context, request) File "/django/template/backends/django.py", line 61, in render
return self.template.render(context) File "/django/template/base.py", line 175, in render
return self._render(context) File "/django/template/base.py", line 167, in _render
return self.nodelist.render(context) File "/django/template/base.py", line 943, in render
bit = node.render_annotated(context) File "/django/template/base.py", line 910, in render_annotated
return self.render(context) File "/django/template/loader_tags.py", line 155, in render
return compiled_parent._render(context) File "/django/template/base.py", line 167, in _render
return self.nodelist.render(context) File "/django/template/base.py", line 943, in render
bit = node.render_annotated(context) File "/django/template/base.py", line 910, in render_annotated
return self.render(context) File "/django/template/loader_tags.py", line 67, in render
result = block.nodelist.render(context) File "/django/template/base.py", line 943, in render
bit = node.render_annotated(context) File "/django/template/base.py", line 910, in render_annotated
return self.render(context) File "/django/template/loader_tags.py", line 194, in render
return template.render(context) File "/django/template/base.py", line 177, in render
return self._render(context) File "/django/template/base.py", line 167, in _render
return self.nodelist.render(context) File "/django/template/base.py", line 943, in render
bit = node.render_annotated(context) File "/django/template/base.py", line 910, in render_annotated
return self.render(context) File "/django/template/base.py", line 993, in render
output = self.filter_expression.resolve(context) File "/django/template/base.py", line 697, in resolve
arg_vals.append(arg.resolve(context)) File "/django/template/base.py", line 802, in resolve
value = self._resolve_lookup(context) File "/django/template/base.py", line 856, in _resolve_lookup
(bit, current)) # missing attribute django.template.base.VariableDoesNotExist: Failed lookup for key [customer] in [{'True': True, 'False': False, 'None': None}, {}, {}, {'company': <Company: My Company>}] [08/May/2018 18:55:25] "GET /companies/my-company/ HTTP/1.1" 500 214439
答案 0 :(得分:2)
您的视图未在模板上下文中设置customer
。也许你想要:
{{ user|is_owner:company }}
请注意,我已通过company
而不是company_id
,因为该代码使用了company.id
。