Контекст django не может найти значение, которое существует при использовании в теге шаблона - PullRequest
0 голосов
/ 08 мая 2018

В моем представлении render я возвращаю следующий контекстный словарь:

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

1 Ответ

0 голосов
/ 08 мая 2018

Ваше представление не устанавливает customer в контексте шаблона. Возможно, вы хотите:

{{ user|is_owner:company }}

Обратите внимание, что я передал company вместо company_id, поскольку тег использует company.id.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...