django-wiki:如何列出根文章下的所有文章

django-wiki: how to list all articles under root article

我是 Django 的新手,目前正在构建一个站点。我已经下载 django-wiki ver 0.4a3 为我的网站安装一个 wiki 应用程序。一切正常,开箱即用。但不是将根文章显示为主页,而是如何显示根文章下所有文章的列表,以便用户只需单击任何子文章即可打开该文章?目前,用户必须单击菜单选项才能向上浏览一个级别或在当前级别浏览以列出该给定级别的所有文章。我觉得这很乏味。我宁愿使用 "Windows Explorer tree-and-branch-like" 导航,例如

root
|_child 1
| |_child 1.1
| |_child 1.2
|
|_child 2
| |_child 2.1
|   |_child 2.1.1
|
|_child 3

请注意,我问的是如何获取根文章下的所有文章列表,而不是如何创建模板来实现树和分支文章导航器。一旦我知道如何获得所有文章的列表,我想我可以实现必要的 HTML 和 CSS 来拥有那种导航器。感谢您的指点。

P.S。我以前尝试过官方 django-wiki google 支持小组,但我认为那里的支持已经死了。我的两个问题都没有得到回答,更不用说阅读了(我只有 1 次观看——这实际上是我的观看次数)。

克里斯

[已解决。]

复制最初位于 wiki/templates/wiki/article.html,并将此副本放入您自己项目的 templates/wiki/ 目录中。在你想放置树目录的地方,添加类似下面几行的内容:

<!-- article.html -->
...
<h4>List of articles</h4>
<ul>
  <!-- Need to get the first article (root) -->
  {% url 'wiki:get' path=urlpath.root.path as rootpath %}
  <li>
    <a href="{{ rootpath }}">
      {{ urlpath.root.article.current_revision.title }}
    </a>
  </li>

  <!-- Now get all the descendent articles of the root -->
  {% for child in urlpath.root.get_descendants %}
    <!-- Don't list articles marked for deletion -->
    {% if not child.is_deleted %}
      {% with ''|center:child.level as range %}
        {% for _ in range %}<ul>{% endfor %}
        {% url 'wiki:get' path=child.path as childpath %}
        <li>
          <a href="{{ childpath }}">
            {{ child.article.current_revision.title }}
          </a>
        </li>
        {% for _ in range %}</ul>{% endfor %}
      {% endwith %}
    {% endif %}
  {% endfor %}
</ul>

上面的代码大家可以看一下。我是 Django 的新手(并且开始使用 django-wiki),所以这可能不是最干净或最有效的方式,但上面的方法对我来说是预期的。

优势:

  • 文章列表始终更新,因此删除所有已删除的文章并显示插入的新文章。

  • tree-like 层次结构直观地显示了所有可用的文章及其相互之间的关系。这种视觉描述比默认的要好得多,默认的甚至很难知道有多少文章存在或嵌套了多深的文章。

缺点:

  • 根据 django-wiki 的源代码,get_descendents 方法是一个昂贵的调用,但对于我的小站点,我没有注意到任何惩罚命中(到目前为止)。

假设您的结构不超过 10 层:

[article_list depth:10]

把这个放在你的根文章中。