Django/Python运行时错误:超过最大递归深度

dbf7pr2w  于 2022-12-28  发布在  Python
关注(0)|答案(4)|浏览(169)

我正在尝试添加一个博客应用到我的Django项目中。当我把所有东西放在一起时,我可以看到我的博客文章页面,但是blogapp/ www.example.com文件中的一些东西urls.py导致我在某个地方得到了一个最大递归错误,我很难找到它。首先,这里是完整的错误消息:

RuntimeError at /admin/
maximum recursion depth exceeded while calling a Python object
Request Method: GET
Request URL:    localhost/admin/  #I edited this due to a posting error
Django Version: 1.4
Exception Type: RuntimeError
Exception Value:    
maximum recursion depth exceeded while calling a Python object
Exception Location: /Users/User/tmp/newproject/DJANGO/lib/python2.7/site-packages/Django-1.4-py2.7.egg/django/utils/translation/trans_real.py in get_language, line 222
Python Executable:  /Users/User/tmp/newproject/DJANGO/bin/python
Python Version: 2.7.1

下面是mysite/ www.example.com中的urlpatterns变量urls.py:

urlpatterns = patterns('',
    url(r'^polls/', include('polls.urls')),
    url(r'^blogapp/', include('blogapp.urls')),
    url(r'^admin/', include(admin.site.urls)),
)

这是我的博客应用程序/urls.py文件:

from django.conf.urls import patterns, include, url
from django.views.generic import ListView
from blogapp.models import Post
urlpatterns = patterns('',
    url(r'^', ListView.as_view(queryset=Post.objects.all().order_by("-created")[:2],
                            template_name="/Users/User/tmp/newproject/DJANGO/mysite/templates/blogapp/blog.htm    l")),     
    url(r'^blog/', include('blogapp.urls')),
)

另外,为了更好地衡量,这是我的blogapp/models.py文件:

from django.db import models

class Post(models.Model):
    '''represents a class instance of a blog entry'''
    title = models.CharField(max_length=100)
    created = models.DateTimeField()
    body = models.TextField()

    def __unicode__(self):
        return self.title
ndasle7k

ndasle7k1#

你似乎把blogapp.urls包含在它自己里面了,听起来不像是个好主意。

kzipqqlq

kzipqqlq2#

问题是django的logout方法在你的view logout方法里,所以它调用自己,永远不会结束。
因此,您可以将视图注销方法重命名为“signout”或类似的名称。
另一种方法是导入django logout,并在logout方法中调用它:从django.contrib.auth导入注销为core_logout

qxsslcnc

qxsslcnc3#

我得到了类似的错误:
递归错误:在比较中超过最大递归深度
在**“保存()"中创建和保存**“Person”对象时,“Person”类中被覆盖,如下所示:

from django.db import models

class Person(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)

    # ...
    
    def save(self, *args, **kwargs): # Here
        person = Person(first_name='Soi', last_name='Tato')
        person.save()

因此,似乎**”person.save()"一次又一次地调用“Person”类中的“保存()"**,最终导致错误。

tnkciper

tnkciper4#

我假设您正在尝试创建成员对象属性

''represents a class instance of a blog entry'''
    title = models.CharField(max_length=100)
    created = models.DateTimeField()
    body = models.TextField()

理想情况下,该函数应位于

def __init__(self):
''represents a class instance of a blog entry'''
    title = models.CharField(max_length=100)
    created = models.DateTimeField()
    body = models.TextField()

相关问题