【问题标题】:Django admindoc not rendering reStructuredTextDjango admindoc 不呈现 reStructuredText
【发布时间】:2013-09-11 09:14:54
【问题描述】:

我正在使用 django admindocs 来编写文档,并且基本功能运行良好(我可以访问文档页面,模型被列为文档,help_text 被包括在内,等等)。

不幸的是,文档字符串中的 reStructuredText 标记被完全忽略,例如

  • 超链接不会转换为超链接
  • 项目符号列表不是项目符号列表
  • Django 标记,例如 :model:appname.ModelName 未解析

我正在使用 Django (1.7) 的 Development Trunk 版本

这是我正在使用的文档字符串的示例:

class Adresse(models.Model):

    u"""Postanschrift

    Wird für 
     - Organisationen 
     - Personen 

    genutzt.

    Siehe auch https://docs.djangoproject.com/en/dev/ref/contrib/contenttypes/#generic-relations 

    """

    object_id    = models.PositiveIntegerField()
    content_type = models.ForeignKey(ContentType)
    of           = generic.GenericForeignKey('content_type', 'object_id' )
    ...

当我将上面的文档字符串内容粘贴到 rest 编辑器(我使用 http://rst.ninjs.org/)时,一切都按预期工作。

转换适用于文档字符串记录方法,例如

def my_method(self):
    """Docstring Heading

    1. Listitem 1
    2. Listitem 2

    refers to :model:`personen.Person`
    """
    pass

正确转换。

我确定,我错过了一些非常明显的东西,不是吗?

【问题讨论】:

    标签: python django restructuredtext docstring


    【解决方案1】:

    那么 admindocs 模块的行为与 Django 1.4 中的相同。

    • 要从您的 python 文件自动生成可导航文档,Python Sphinx https://www.sphinx-doc.org 可能是更好的方法,将生成的文件放在另一个伪静态文件夹中。
      为此,请将 sphinx 文档复制到模板文件夹并添加自定义 urls(..)view,以提供对设置为仅限员工的限制的文件的访问(例如通过规范装饰器 @987654324 @ 和 user_pases_test

    其他解决方案:

    不幸的是,有关首次使用的文档有些缺乏,例如settings.py 参数RESTRUCTUREDTEXT_FILTER_SETTINGS

    请放心,为了激活这些过滤器,django.contrib.markup' 将添加到 settings.py 中的 INSTALLED_APPS 设置和模板中的 {% load markup %} 设置中。

    您应该安装了docutils。如果未安装,您将不会收到错误消息。 linux配合bash使用时,输入:

    if [[ -z `pip freeze | grep docutils` ]];  then sudo easy_install docutils;fi;
    

    直接渲染reStructuredText:

    from django import template

    class Adresse(models.Model):
        doc = u"""Postanschrift
    
        Wird für 
         - Organisationen 
         - Personen 
        """
    t = template.Template('{% load markup %}{{ contentstr|restructuredtext }}')
    c = template.Context({'contentstr': doc})
    __doc__ = t.render(c)
    

    您可以通过循环遍历所有 [相关] 模型及其 __docs__ 属性来自动执行此操作,随后将字符串呈现为 reStructuredText,如下所示:

    from django.conf import settings
    from django.db.models import get_app, get_models
    from django import template
    
    for appname in settings.INSTALLED_APPS:
      app = get_app(appname )
      for model in get_models(app):
        if hasattr(model, '__doc__') and model.__doc__ != "":
          t = template.Template('{% load markup %}{{ contentstr|restructuredtext }}')
          c = template.Context({'contentstr': model.__doc__})
          model.__doc__ = t.render(c)
    

    【讨论】:

    • 感谢您的详尽回答!我会深入研究提到的选项,并就对我有用的方法提供反馈!
    猜你喜欢
    • 2019-11-22
    • 2018-05-31
    • 1970-01-01
    • 2012-12-27
    • 2012-04-01
    • 2017-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多