Django&降价问题:'NoneType'对象没有属性'strip'

时间:2009-09-22 15:37:49

标签: django markdown

我有关于markdown和django集成的问题。在我安装markdown并将我的模型类更改为:

之后
class Page(models.Model):
    class Translation(multilingual.Translation):
        title = models.CharField(verbose_name="Title", max_length=30,
                                 help_text="Put a title (max. 30 chars) of your page -e.g. About, CEO, Contact etc...")
        content_markdown = models.TextField(verbose_name="Markdown Content",
                                            help_text="Use Markdown syntax here.")
        content = models.TextField(verbose_name="Page content as HTML", 
                                   blank=True, null=True,
                                   help_text="You don't have to touch here.")
    slug = models.SlugField(verbose_name="Slug",
                            help_text="Put here the name of your page without space -e.g. research-development")

    class Meta:
        verbose_name_plural = "Pages"
        ordering = ['id']

    def __unicode__(self):
        return self.title

    def save(self):
        import markdown
        self.content = markdown.markdown(self.content_markdown)
        super(Page, self).save()

    def get_absolute_url(self):
        # return "/%s/%s" % (self.menu.slug, self.slug)
        return "/%s" % self.slug

我得到了这些追溯:

Traceback:
File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/base.py" in get_response
  92.                 response = callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/contrib/admin/options.py" in wrapper
  226.                 return self.admin_site.admin_view(view)(*args, **kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/views/decorators/cache.py" in _wrapped_view_func
  44.         response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/contrib/admin/sites.py" in inner
  186.             return view(request, *args, **kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/db/transaction.py" in _commit_on_success
  240.                 res = func(*args, **kw)
File "/usr/local/lib/python2.6/dist-packages/django/contrib/admin/options.py" in add_view
  734.                 self.save_model(request, new_object, form, change=False)
File "/usr/local/lib/python2.6/dist-packages/django/contrib/admin/options.py" in save_model
  557.         obj.save()
File "/media/DATA/Programming/pyworkspace/djangoprojects/youngjin/../youngjin/archive/models.py" in save
  37.       self.content = markdown.markdown(self.content_markdown)
File "/usr/local/lib/python2.6/dist-packages/Markdown-2.0.1-py2.6.egg/markdown/__init__.py" in markdown
  587.     return md.convert(text)
File "/usr/local/lib/python2.6/dist-packages/Markdown-2.0.1-py2.6.egg/markdown/__init__.py" in convert
  370.         if not source.strip():

Exception Type: AttributeError at /admin/archive/page/add/
Exception Value: 'NoneType' object has no attribute 'strip'

我无法弄清问题是什么。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我猜测self.content_markdown在某些情况下可能为空(但我不确定为什么,因为默认情况下应该返回空字符串而不是null值,如果没有另外指定的话)。

无论如何,你可以尝试以下方式,只是为了确定吗?

self.content = markdown.markdown(self.content_markdown or u'')