У объекта 'str' нет атрибута 'стратегии' - django-oscar
У меня проблемы с этой ошибкой в django-oscar.
Я читал, что Promotion_tags.py не обрабатывал запрос в контексте. Но я видел код oscar внутри моего virtualenv, у него есть запрос внутри контекста
но странность в том, что функции из Promotion_tags.py не вызываются, поэтому в контексте отсутствует объект запроса. но я не уверен, что это единственное место, где объект запроса изменен / добавлен в контекст.
вот код, которому нужен объект запроса:
@register.simple_tag
def purchase_info_for_product(request, product):
if product.is_parent:
return request.strategy.fetch_for_parent(product)
return request.strategy.fetch_for_product(product)
Я использую Джанго-Оскар 1-6 и Джанго 1.11.
вот полный трекбек:
2018-08-17 12:52:53,567 django.request ERROR Internal Server Error: /category/beauty-wellness_9/
Traceback (most recent call last):
File "/home/vagrant/.virtualenvs/goldmarie/lib/python3.6/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/home/vagrant/.virtualenvs/goldmarie/lib/python3.6/site-packages/django/core/handlers/base.py", line 217, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/vagrant/.virtualenvs/goldmarie/lib/python3.6/site-packages/django/core/handlers/base.py", line 215, in _get_response
response = response.render()
File "/home/vagrant/.virtualenvs/goldmarie/lib/python3.6/site-packages/django/template/response.py", line 107, in render
self.content = self.rendered_content
File "/home/vagrant/.virtualenvs/goldmarie/lib/python3.6/site-packages/django/template/response.py", line 84, in rendered_content
content = template.render(context, self._request)
File "/home/vagrant/.virtualenvs/goldmarie/lib/python3.6/site-packages/django/template/backends/django.py", line 66, in render
return self.template.render(context)
File "/home/vagrant/.virtualenvs/goldmarie/lib/python3.6/site-packages/django/template/base.py", line 207, in render
return self._render(context)
File "/home/vagrant/.virtualenvs/goldmarie/lib/python3.6/site-packages/django/test/utils.py", line 107, in instrumented_test_render
return self.nodelist.render(context)
File "/home/vagrant/.virtualenvs/goldmarie/lib/python3.6/site-packages/django/template/base.py", line 990, in render
bit = node.render_annotated(context)
File "/home/vagrant/.virtualenvs/goldmarie/lib/python3.6/site-packages/django/template/base.py", line 957, in render_annotated
return self.render(context)
File "/home/vagrant/.virtualenvs/goldmarie/lib/python3.6/site-packages/django/template/loader_tags.py", line 177, in render
return compiled_parent._render(context)
File "/home/vagrant/.virtualenvs/goldmarie/lib/python3.6/site-packages/django/test/utils.py", line 107, in instrumented_test_render
return self.nodelist.render(context)
File "/home/vagrant/.virtualenvs/goldmarie/lib/python3.6/site-packages/django/template/base.py", line 990, in render
bit = node.render_annotated(context)
File "/home/vagrant/.virtualenvs/goldmarie/lib/python3.6/site-packages/django/template/base.py", line 957, in render_annotated
return self.render(context)
File "/home/vagrant/.virtualenvs/goldmarie/lib/python3.6/site-packages/django/template/loader_tags.py", line 177, in render
return compiled_parent._render(context)
File "/home/vagrant/.virtualenvs/goldmarie/lib/python3.6/site-packages/django/test/utils.py", line 107, in instrumented_test_render
return self.nodelist.render(context)
File "/home/vagrant/.virtualenvs/goldmarie/lib/python3.6/site-packages/django/template/base.py", line 990, in render
bit = node.render_annotated(context)
File "/home/vagrant/.virtualenvs/goldmarie/lib/python3.6/site-packages/django/template/base.py", line 957, in render_annotated
return self.render(context)
File "/home/vagrant/.virtualenvs/goldmarie/lib/python3.6/site-packages/django/template/loader_tags.py", line 177, in render
return compiled_parent._render(context)
File "/home/vagrant/.virtualenvs/goldmarie/lib/python3.6/site-packages/django/test/utils.py", line 107, in instrumented_test_render
return self.nodelist.render(context)
File "/home/vagrant/.virtualenvs/goldmarie/lib/python3.6/site-packages/django/template/base.py", line 990, in render
bit = node.render_annotated(context)
File "/home/vagrant/.virtualenvs/goldmarie/lib/python3.6/site-packages/django/template/base.py", line 957, in render_annotated
return self.render(context)
File "/home/vagrant/.virtualenvs/goldmarie/lib/python3.6/site-packages/django/template/loader_tags.py", line 72, in render
result = block.nodelist.render(context)
File "/home/vagrant/.virtualenvs/goldmarie/lib/python3.6/site-packages/django/template/base.py", line 990, in render
bit = node.render_annotated(context)
File "/home/vagrant/.virtualenvs/goldmarie/lib/python3.6/site-packages/django/template/base.py", line 957, in render_annotated
return self.render(context)
File "/home/vagrant/.virtualenvs/goldmarie/lib/python3.6/site-packages/django/template/loader_tags.py", line 72, in render
result = block.nodelist.render(context)
File "/home/vagrant/.virtualenvs/goldmarie/lib/python3.6/site-packages/django/template/base.py", line 990, in render
bit = node.render_annotated(context)
File "/home/vagrant/.virtualenvs/goldmarie/lib/python3.6/site-packages/django/template/base.py", line 957, in render_annotated
return self.render(context)
File "/home/vagrant/.virtualenvs/goldmarie/lib/python3.6/site-packages/django/template/loader_tags.py", line 72, in render
result = block.nodelist.render(context)
File "/home/vagrant/.virtualenvs/goldmarie/lib/python3.6/site-packages/django/template/base.py", line 990, in render
bit = node.render_annotated(context)
File "/home/vagrant/.virtualenvs/goldmarie/lib/python3.6/site-packages/django/template/base.py", line 957, in render_annotated
return self.render(context)
File "/home/vagrant/.virtualenvs/goldmarie/lib/python3.6/site-packages/django/template/base.py", line 1040, in render
output = self.filter_expression.resolve(context)
File "/home/vagrant/.virtualenvs/goldmarie/lib/python3.6/site-packages/django/template/base.py", line 708, in resolve
obj = self.var.resolve(context)
File "/home/vagrant/.virtualenvs/goldmarie/lib/python3.6/site-packages/django/template/base.py", line 849, in resolve
value = self._resolve_lookup(context)
File "/home/vagrant/.virtualenvs/goldmarie/lib/python3.6/site-packages/django/template/base.py", line 911, in _resolve_lookup
current = current()
File "/home/vagrant/.virtualenvs/goldmarie/lib/python3.6/site-packages/django/template/loader_tags.py", line 86, in super
return mark_safe(self.render(self.context))
File "/home/vagrant/.virtualenvs/goldmarie/lib/python3.6/site-packages/django/template/loader_tags.py", line 72, in render
result = block.nodelist.render(context)
File "/home/vagrant/.virtualenvs/goldmarie/lib/python3.6/site-packages/django/template/base.py", line 990, in render
bit = node.render_annotated(context)
File "/home/vagrant/.virtualenvs/goldmarie/lib/python3.6/site-packages/django/template/base.py", line 957, in render_annotated
return self.render(context)
File "/home/vagrant/.virtualenvs/goldmarie/lib/python3.6/site-packages/django/template/defaulttags.py", line 322, in render
return nodelist.render(context)
File "/home/vagrant/.virtualenvs/goldmarie/lib/python3.6/site-packages/django/template/base.py", line 990, in render
bit = node.render_annotated(context)
File "/home/vagrant/.virtualenvs/goldmarie/lib/python3.6/site-packages/django/template/base.py", line 957, in render_annotated
return self.render(context)
File "/home/vagrant/.virtualenvs/goldmarie/lib/python3.6/site-packages/django/template/defaulttags.py", line 216, in render
nodelist.append(node.render_annotated(context))
File "/home/vagrant/.virtualenvs/goldmarie/lib/python3.6/site-packages/django/template/base.py", line 957, in render_annotated
return self.render(context)
File "/home/vagrant/.virtualenvs/goldmarie/lib/python3.6/site-packages/django/template/library.py", line 245, in render
return t.render(new_context)
File "/home/vagrant/.virtualenvs/goldmarie/lib/python3.6/site-packages/django/template/base.py", line 209, in render
return self._render(context)
File "/home/vagrant/.virtualenvs/goldmarie/lib/python3.6/site-packages/django/test/utils.py", line 107, in instrumented_test_render
return self.nodelist.render(context)
File "/home/vagrant/.virtualenvs/goldmarie/lib/python3.6/site-packages/django/template/base.py", line 990, in render
bit = node.render_annotated(context)
File "/home/vagrant/.virtualenvs/goldmarie/lib/python3.6/site-packages/django/template/base.py", line 957, in render_annotated
return self.render(context)
File "/home/vagrant/.virtualenvs/goldmarie/lib/python3.6/site-packages/django/template/loader_tags.py", line 63, in render
result = self.nodelist.render(context)
File "/home/vagrant/.virtualenvs/goldmarie/lib/python3.6/site-packages/django/template/base.py", line 990, in render
bit = node.render_annotated(context)
File "/home/vagrant/.virtualenvs/goldmarie/lib/python3.6/site-packages/django/template/base.py", line 957, in render_annotated
return self.render(context)
File "/home/vagrant/.virtualenvs/goldmarie/lib/python3.6/site-packages/django/template/loader_tags.py", line 63, in render
result = self.nodelist.render(context)
File "/home/vagrant/.virtualenvs/goldmarie/lib/python3.6/site-packages/django/template/base.py", line 990, in render
bit = node.render_annotated(context)
File "/home/vagrant/.virtualenvs/goldmarie/lib/python3.6/site-packages/django/template/base.py", line 957, in render_annotated
return self.render(context)
File "/home/vagrant/.virtualenvs/goldmarie/lib/python3.6/site-packages/django/template/loader_tags.py", line 216, in render
return template.render(context)
File "/home/vagrant/.virtualenvs/goldmarie/lib/python3.6/site-packages/django/template/base.py", line 209, in render
return self._render(context)
File "/home/vagrant/.virtualenvs/goldmarie/lib/python3.6/site-packages/django/test/utils.py", line 107, in instrumented_test_render
return self.nodelist.render(context)
File "/home/vagrant/.virtualenvs/goldmarie/lib/python3.6/site-packages/django/template/base.py", line 990, in render
bit = node.render_annotated(context)
File "/home/vagrant/.virtualenvs/goldmarie/lib/python3.6/site-packages/django/template/base.py", line 957, in render_annotated
return self.render(context)
File "/home/vagrant/.virtualenvs/goldmarie/lib/python3.6/site-packages/django/template/library.py", line 203, in render
output = self.func(*resolved_args, **resolved_kwargs)
File "/home/vagrant/goldmarie/goldmarie/templatetags/gm_purchase_info_tags.py", line 13, in purchase_info_for_product
return request.strategy.fetch_for_product(product)
AttributeError: 'str' object has no attribute 'strategy'
здесь происходит исключение
1 ответ
Смотрите здесь, как передать контекст шаблона в теги шаблона:
@register.simple_tag(takes_context=True)
def purchase_info_for_product(context, product):
request = context['request']
if product.is_parent:
return request.strategy.fetch_for_parent(product)
return request.strategy.fetch_for_product(product)
С вашим оригинальным кодом, вы передаете product
в качестве request
параметр.
В качестве альтернативы, если тег встроен в Оскар, передайте запрос:
{% purchase_info_for_product request product %}
Передайте аргумент стратегии в вашей функции. Но в качестве основной проблемы вы вносите изменения в приложение Basket и не наследуете стратегию, разработанную для корзины. Это все, что я могу вам сказать тем, что вижу в вашем вопросе. Если вы можете опубликовать полный код, связанный с вашим приложением, вы разветвились с полной ошибкой, создав "суть". Это будет довольно легко отлаживать.:-)