django - django 上下文找不到在模板标签中使用时存在的值
问题描述
在我的视图渲染中,我返回以下上下文字典:
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
解决方案
您的视图未customer
在模板上下文中设置。也许你想要:
{{ user|is_owner:company }}
请注意,我已经通过company
而不是company_id
因为标签使用company.id
.
推荐阅读
- python - Django站点地图'Post'对象没有属性'get_absolute_url'?
- go - 一个函数返回一个变量的地址......最终是一个不同的变量?
- java - 使用片段时无法启动活动 ComponentInfo
- python - 如何计算高级瓷砖上的点数?
- database - Wordpress 和数据库基础知识。从哪儿开始?
- php - 如果被 Laravel 破坏,如何执行不会停止其余部分的代码
- arrays - 如何在 JSONB 数组的元素上使用 LIKE?
- actionscript-3 - Adobe Flash/Animate - Actionscript 3 中等效的转换点
- git - 将 git 添加到 Windows 上的路径会中断 Make?
- java - 接口不从 Last.fm Api 检索数据