'str'对象没有属性'strategy'-django-oscar

时间:2018-08-17 06:59:52

标签: python django django-oscar

我在django-oscar中遇到了这个错误。

我已经了解到promotion_tags.py没有在上下文中呈现请求。但是我已经在我的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)

我正在使用django-oscar 1-6和django 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'

这是发生异常的地方

enter image description here

2 个答案:

答案 0 :(得分:0)

在此处查看how to pass the template context into template tags

@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参数传入。

或者,如果代码是Oscar内置的,则传递请求:

{% purchase_info_for_product request product %}

答案 1 :(得分:0)

在函数中传递策略参数。但是,作为问题的核心,您正在更改购物篮应用程序,并且没有继承为购物篮设计的策略。 这就是我在问题中所看到的全部。如果您可以发布与您的应用程序相关的完整代码,则可以通过创建“要点”来分叉,并获得完整的错误。这将非常容易调试。 :-)