【问题标题】:Django maximum recursion depth exceeded超出 Django 最大递归深度
【发布时间】:2011-03-31 03:31:30
【问题描述】:

我正在使用 Django 构建一个小型 Web 项目,该项目有一个模型 (Image),其中包含 ImageField。当我尝试使用管理界面上传图片时,我遇到了这个问题(个人识别信息被删除):

RuntimeError at /admin/main/image/add/

maximum recursion depth exceeded

Request Method:     POST
Request URL:    http://x.x.x.x:8080/blog/admin/main/image/add/
Django Version:     1.2.1
Exception Type:     RuntimeError
Exception Value:    

maximum recursion depth exceeded

Exception Location:     /extra/django/blog/main/models.py in __unicode__, line 26
Python Executable:  /usr/bin/python
Python Version:     2.4.3
Python Path:    ['/extra/django', '/usr/lib/python2.4/site-packages/setuptools-0.6c11-py2.4.egg', '/usr/lib/python2.4/site-packages/MySQL_python-1.2.3-py2.4-linux-i686.egg', '/usr/lib/python24.zip', '/usr/lib/python2.4', '/usr/lib/python2.4/plat-linux2', '/usr/lib/python2.4/lib-tk', '/usr/lib/python2.4/lib-dynload', '/usr/lib/python2.4/site-packages', '/usr/lib/python2.4/site-packages/Numeric', '/usr/lib/python2.4/site-packages/PIL', '/usr/lib/python2.4/site-packages/gtk-2.0']
Server time:    Tue, 17 Aug 2010 13:30:20 -0400

这是我的models.py的一部分:

class Image(models.Model):
image = models.ImageField(upload_to='uploads/blog_images')
caption = models.CharField(max_length=300)
post = models.ForeignKey('Post')
thumbWidth = models.IntegerField(blank=True,null=True)
thumbHeight = models.IntegerField(blank=True,null=True)
def printTag(self, newClass=''):
    str = '<img '
    if newClass is not '':
        str = str + 'class="%s" ' %newClass
    if self.thumbWidth is not None and self.thumbHeight is not None:
        str += 'width="%i" height="%i" ' %(self.thumbWidth,self.thumbHeight)
    str = str + 'src="%s" ' %self.image
    str = str + '>%s</img>' %self.caption
    return str
def __unicode__(self):
    return self.printTag(self)

第 26 行是 unicode 内的唯一行。我有额外的功能(printTag),所以我可以选择是否打印带有“类”属性的 HTML 标记,默认情况下没有该属性。为什么我上传图片时会重复?

【问题讨论】:

  • 请修正缩进。您的代码很难阅读。

标签: python django apache recursion


【解决方案1】:

你需要return self.printTag()而不是return self.printTag(self)

【讨论】:

  • 为了更清楚(现在我回到我的电脑前),您的 printTag() 方法属于模型,因此隐含地将 self 作为第一个参数。通过调用 self.printTag(self),这意味着对象也被接受为 second 参数,这意味着您的代码认为它是 newClass,然后尝试拼接到 再次以 unicode 表示对象(作为 newClass),这会导致递归。
猜你喜欢
  • 2017-08-09
  • 2013-02-20
  • 2013-12-01
  • 2011-09-19
  • 2019-01-18
  • 2013-03-03
  • 2017-03-24
  • 2011-12-31
相关资源
最近更新 更多