13

我正在尝试将博客应用程序添加到我的 Django 项目中。当我将所有内容放在一起时,我可以看到我的博客文章页面,但是 blogapp/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/urls.py 的 urlpatterns 变量:

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

这是我的 blogapp/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
4

3 回答 3

34

您似乎在其内部包含 blogapp.urls 。听起来不是个好主意。

于 2012-12-15T13:52:40.877 回答
3

问题是 django 注销方法在您的视图注销方法中。所以它呼唤自己,永无止境。

因此,您可以将视图注销方法重命名为“signout”或类似名称。

其他方法是使用其他名称导入 django logout,如下所示,并在您的注销方法中调用它: from django.contrib.auth import logout as core_logout

于 2016-04-08T00:44:11.273 回答
-2

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

''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()
于 2012-12-15T13:50:55.560 回答