Django 超出最大递归深度

发布于 2024-09-14 22:02:46 字数 1988 浏览 4 评论 0原文

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

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),因此我可以选择是否打印带有“class”属性的 HTML 标记,默认情况下不带该属性。为什么我上传图片时会出现重复?

I'm building a small web project using Django that has one model (Image) that contains an ImageField. When I try to upload an image using the admin interface I am presented with this problem (personally identifying information removed):

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

And this is a portion of my 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)

Line 26 is the only line inside unicode. I have the extra function (printTag) so I can choose whether or not to print the HTML tag with a "class" attribute with the default being without the attribute. Why is it recursing when I upload an image?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╰◇生如夏花灿烂 2024-09-21 22:02:46

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

You need return self.printTag() not return self.printTag(self)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文