Atma
Atma

Reputation: 29767

django context can't find value which exists when used in template tag

In my view render I return the following context dictionary:

company = get_object_or_404(Company, slug=slug)
context = {
               'company':company,

    }

return render(request, 'company-detail.html', context)

I use this in a template tag to check ownership:

@register.filter(name='is_owner')
def is_owner(user, company):

    if user.customer.company.id == company.id:
        return True

    else:
        return False

I try to see the output of the tag in my template:

{{user|is_owner:customer.id}}

I get the following error:

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

Upvotes: 0

Views: 166

Answers (1)

Alasdair
Alasdair

Reputation: 308779

Your view doesn’t set customer in the template context. Perhaps you want:

{{ user|is_owner:company }}

Note that I’ve passed company instead of company_id because the tag uses company.id.

Upvotes: 2

Related Questions