姜戈&降价问题:“NoneType”对象没有属性“strip”;

发布于 2024-08-05 00:32:18 字数 3020 浏览 7 评论 0原文

我有一个关于 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'

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

I have a problem about markdown and django integration. After I installed markdown and change my model class to:

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

I got these traceback:

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'

I cannot really figure out what is the problem. Any idea?

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

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

发布评论

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

评论(1

不必了 2024-08-12 00:32:18

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

无论如何,您可以尝试以下操作来确定吗?

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

I would guess that self.content_markdown might be null in some cases (but I am not sure why, because the default should be to return empty strings instead of null-values if not specified otherwise).

Anyway, can you try the following, just to be sure?

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