Django RecursionError

时间:2017-02-27 23:00:31

标签: django python-3.x django-models django-templates

在/ cart /最大递归深度处的RecursionError在我看来,错误位于函数的某个地方

def get_total_price(self):
        return sum(Decimal(item['price']) * item['quantity'] for item in self.cart.values())

    def clear(self):
        # usunięcie koszyka
        del self.session[settings.CART_SESSION_ID]
        self.session.modified = True

    @property
    def delivery(self):
        if self.delivery_id:
            return Delivery.objects.get(id=self.delivery)
        return None

    def get_delivery(self):
        if self.delivery:
            return (self.delivery.cost / Decimal('100')) * self.get_total_price()

    def get_total_price_delivery(self):
        return self.get_total_price() - self.get_delivery() 

跟踪

Internal Server Error: /cart/
    Traceback (most recent call last):
      File "/home/krystian/.virtualenvs/vpw/lib/python3.5/site-packages/django/core/handlers/exception.py", line 39, in inner
        response = get_response(request)
      File "/home/krystian/.virtualenvs/vpw/lib/python3.5/site-packages/django/core/handlers/base.py", line 187, in _get_response
        response = self.process_exception_by_middleware(e, request)
      File "/home/krystian/.virtualenvs/vpw/lib/python3.5/site-packages/django/core/handlers/base.py", line 185, in _get_response
        response = wrapped_callback(request, *callback_args, **callback_kwargs)
      File "/home/krystian/Project/villain/villain/cart/views.py", line 45, in cart_detail
        'delivery_apply_form': delivery_apply_form})
      File "/home/krystian/.virtualenvs/vpw/lib/python3.5/site-packages/django/shortcuts.py", line 30, in render
        content = loader.render_to_string(template_name, context, request, using=using)
      File "/home/krystian/.virtualenvs/vpw/lib/python3.5/site-packages/django/template/loader.py", line 68, in render_to_string
        return template.render(context, request)
      File "/home/krystian/.virtualenvs/vpw/lib/python3.5/site-packages/django/template/backends/django.py", line 66, in render
        return self.template.render(context)
      File "/home/krystian/.virtualenvs/vpw/lib/python3.5/site-packages/django/template/base.py", line 208, in render
        return self._render(context)
      File "/usr/lib/python3.5/contextlib.py", line 77, in __exit__
        self.gen.throw(type, value, traceback)
      File "/home/krystian/.virtualenvs/vpw/lib/python3.5/site-packages/django/template/context.py", line 240, in bind_template
        yield
      File "/home/krystian/.virtualenvs/vpw/lib/python3.5/site-packages/django/template/base.py", line 208, in render
        return self._render(context)
      File "/home/krystian/.virtualenvs/vpw/lib/python3.5/site-packages/django/template/base.py", line 199, in _render
        return self.nodelist.render(context)
      File "/home/krystian/.virtualenvs/vpw/lib/python3.5/site-packages/django/template/base.py", line 994, in render
        bit = node.render_annotated(context)
      File "/home/krystian/.virtualenvs/vpw/lib/python3.5/site-packages/django/template/base.py", line 961, in render_annotated
        return self.render(context)
      File "/home/krystian/.virtualenvs/vpw/lib/python3.5/site-packages/django/template/loader_tags.py", line 174, in render
        return compiled_parent._render(context)
      File "/home/krystian/.virtualenvs/vpw/lib/python3.5/site-packages/django/template/base.py", line 199, in _render
        return self.nodelist.render(context)
      File "/home/krystian/.virtualenvs/vpw/lib/python3.5/site-packages/django/template/base.py", line 994, in render
        bit = node.render_annotated(context)
      File "/home/krystian/.virtualenvs/vpw/lib/python3.5/site-packages/django/template/base.py", line 961, in render_annotated
        return self.render(context)
      File "/home/krystian/.virtualenvs/vpw/lib/python3.5/site-packages/django/template/loader_tags.py", line 70, in render
        result = block.nodelist.render(context)
      File "/home/krystian/.virtualenvs/vpw/lib/python3.5/site-packages/django/template/base.py", line 994, in render
        bit = node.render_annotated(context)
      File "/home/krystian/.virtualenvs/vpw/lib/python3.5/site-packages/django/template/base.py", line 961, in render_annotated
        return self.render(context)
      File "/home/krystian/.virtualenvs/vpw/lib/python3.5/site-packages/django/template/base.py", line 1044, in render
        output = self.filter_expression.resolve(context)
      File "/home/krystian/.virtualenvs/vpw/lib/python3.5/site-packages/django/template/base.py", line 711, in resolve
        obj = self.var.resolve(context)
      File "/home/krystian/.virtualenvs/vpw/lib/python3.5/site-packages/django/template/base.py", line 852, in resolve
        value = self._resolve_lookup(context)
      File "/home/krystian/.virtualenvs/vpw/lib/python3.5/site-packages/django/template/base.py", line 915, in _resolve_lookup
        current = current()
      File "/home/krystian/Project/villain/villain/cart/cart.py", line 93, in get_delivery
        if self.delivery:
      File "/home/krystian/Project/villain/villain/cart/cart.py", line 89, in delivery
        return Delivery.objects.get(id=self.delivery)
      File "/home/krystian/Project/villain/villain/cart/cart.py", line 89, in delivery
        return Delivery.objects.get(id=self.delivery)
      File "/home/krystian/Project/villain/villain/cart/cart.py", line 89, in delivery
        return Delivery.objects.get(id=self.delivery)
      File "/home/krystian/Project/villain/villain/cart/cart.py", line 89, in delivery
        return Delivery.objects.get(id=self.delivery)
      File "/home/krystian/Project/villain/villain/cart/cart.py", line 89, in delivery
        return Delivery.objects.get(id=self.delivery)
      File "/home/krystian/Project/villain/villain/cart/cart.py", line 89, in delivery
        return Delivery.objects.get(id=self.delivery)
      File "/home/krystian/Project/villain/villain/cart/cart.py", line 89, in delivery
        return Delivery.objects.get(id=self.delivery)
      File "/home/krystian/Project/villain/villain/cart/cart.py", line 
89,交货期             return Delivery.objects.get(id = self.delivery)           文件" /home/krystian/.virtualenvs/vpw/lib/python3.5/site-packages/django/db/models/manager.py" ;,第193行,获取             如果cls._meta.swapped:         RecursionError:超出最大递归深度

2 个答案:

答案 0 :(得分:1)

您有一个名为delivery的媒体资源。在该方法中,使用self.delivery的值查询数据库,该值引用您已经存在的属性,因此在不停止的情况下递归调用自身。

我不确定你要在那里引用什么,但你可能有一个同名的字段和属性 - 你应该重命名其中一个。

答案 1 :(得分:0)

递归发生在应用程序文件夹旁边的 urls.py 中。例如,如果您的项目名称是“farm”,项目中的应用名称是“animal”,并且您在定义应用路径时使用了项目名称,则通常会发生这种情况。 在项目文件夹中定义 urls 模式的正确示例: 项目名称:农场 应用名称:动物

urlpatterns = [
path('animal/', include("animal.urls")
]

上面的意思是你已经安装了一个名为animal的应用,当用户在网址后输入/animal时,应该被带到animal app urls模块进行路由。

错误示例: 示例信息:项目名称=农场,应用名称=动物

urlpatterns = [ path('farm/', include('farm.urls')]

上面的路径告诉 django 查看项目文件夹中的农场董事会内部,并且当时正在浏览农场文件夹(不是应用程序而是项目)内的 urls.py 从而过去和结束。