【问题标题】:django-wiki: how to list all articles under root articledjango-wiki:如何列出根文章下的所有文章
【发布时间】:2018-08-16 09:58:57
【问题描述】:

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

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

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

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

克里斯。

【问题讨论】:

    标签: python django django-wiki


    【解决方案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),所以这可能不是最干净或最有效的方法,但上述方法对我来说是预期的。

    优势:

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

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

    缺点:

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

    【讨论】:

      【解决方案2】:

      假设你的结构不超过 10 深:

      [article_list depth:10]
      

      把它放在你的根文章中。

      【讨论】:

        猜你喜欢
        • 2022-12-15
        • 2011-04-27
        • 1970-01-01
        • 2020-07-10
        • 2023-03-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多