4

我正在尝试使用构建一个博客lektor CMS为此..我需要一个标签系统,在搜索后我在 lektor docs上找到了一个名为lektor 的插件lektor-tags

我遵循文档的每一步,费了很大力气,甚至访问了 github repo以查看文档中是否还有其他内容。

我的问题是当我尝试访问时localhost:5000/{the_tag_name}说就像localhost:5000/python我总是404 Not Found

在服务器上找不到请求的 URL。如果您手动输入了 URL,请检查您的拼写并重试。

这是我到目前为止所做的:

使用lektor-tags

  1. 我将博客帖子的路线更改为/posts而不是/blog.

  2. models/blog.ini向in添加了 slug 格式[children]

     [children]
     model = blog-post
     order_by = pub_date, title
     slug_format = {{ (this.pub_date|dateformat('YYYY/M/') if this.pub_date) ~ this._id }}
    
  3. 创建了 3 个帖子,一切正常。

此时我想使用标签系统,所以我选择使用lektor-tags,我所做的是:

  1. 安装

    lektor plugins add lektor-tags
    
  2. configs/tags.ini使用此配置创建:

    parent = /
    url_path = {{ this.parent.url_path }}{{ tag }}
    tags_field = tags
    ignore_missing = true
    template = tags.html
    
  3. templates/tags.html使用以下内容创建:

        {% extends "layout.html" %}
        {% block title %}{{ this.title }}{% endblock %}
        {% block body %}
    
        <div class="container mt-3">
            <b>Tag: {{ this.tag }}</b>
            <b>Items:</b>
            <ul>
                  {% for i in this.items %}
                  <li><a href="{{ i|url }}">{{ i._id }}</a></li>
                  {% else %}
                  <li><em>No items.</em></li>
                  {% endfor %}
            </ul>
        </div> 
        {% endblock %}
    
  4. 编辑models/blog-post.ini并添加:

       [fields.tags]
       type = strings
    
  5. templates/blog-post.html我添加以下内容以显示指向包含具有特定标签的所有帖子列表的页面的链接:

        {% if this.tags %}
        <ul>
             {% for t in this.tags -%}
             <li>
                  <a href="{{ ('/' ~ t.lower())|url }}">
                  All posts tagged {{ t }}
                  </a>
             </li>
             {% endfor %}
        </ul>
        {% endif %}
    
  6. 最后我更新了一个帖子以包含来自管理员的一些标签,并确保它在content.lr那个帖子中。所以我停止了 lektor 开发服务器并再次运行它lektor servor,没有出现任何错误。

标签的链接在帖子中,但是当我单击并点击链接时,例如localhost:5000/python我得到的 python 标签404 Not Found。我是 lektor 的新手。我想知道我做错了什么,我怎样才能让它正常工作?


注意:我使用的其他插件是lektor-minifylektor-disqus-comments这些插件的文档很简单,我没有感到困惑,但是当谈到这个特定的插件时,我感到困惑,挣扎:文档不是那么好和解释,我完全迷失了!


更新

我创建了一个github 存储库,其中包含代码以及到目前为止我所做的事情,因此您可以轻松地复制它。


更新 2

我设法使它正常工作,请参阅下面的答案,但是现在我想知道如何将根设置为父级,换句话说,如何编辑此表达式:

<a href="{{ ('/posts@tag/' ~ t.lower())|url }}">

为每个博客文章的标签生成一个源路径,但使用 root 作为父级。如您所见,我尝试了这个:

<a href="{{ ('/' ~ t.lower())|url }}">

但这不能正常工作。

值得一提的是,lektor 使用jinja2模板语言。

4

1 回答 1

3

所以基本上是我做错了,因为我想像这样使用根作为父级tags.ini

    parent = /

我最终将表达式更改'/blog@tag/' ~ t.lower()blog-post.html

    <a href="{{ ('/' ~ t.lower())|url }}">

使其无法为每个博客文章的标签生成源路径

我改变和工作的是

  1. 我选择posts成为父母,更新configs/tags.ini为:

    parent = /posts
    url_path = {{ this.parent.url_path }}{{ tag }}
    tags_field = tags
    ignore_missing = true
    template = tags.html
    
  2. 更新templates/blog-post.html

    {% if this.tags %}
    <ul>
        {% for t in this.tags -%}
        <li>
            # '/posts@tag/' ~ t.lower() because i changed the route of blog to posts
            <a href="{{ ('/posts@tag/' ~ t.lower())|url }}">
            All posts tagged {{ t }}
            </a>
        </li>
        {% endfor %}
    </ul>
    {% endif %}
    

运行lektor clean --yeslekor server一切正常,包括标签系统。

于 2021-06-09T01:07:36.943 回答