Lektor CMS:无法使lektortags正常工作,在服务器上找不到请求的URL

2024-05-23 15:23:34 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我遵循文档的每一步,努力了很多,甚至访问了githubrepo,看看文档中是否还有其他未包含的内容

我的问题是,当我试图访问localhost:5000/{the_tag_name}时,比如说localhost:5000/python,我总是听到一个404 Not Found这样说

The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.

这就是我目前所做的:

之前使用lektor-tags

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

  2. [children]中的models/blog.ini中添加了slug格式

     [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 dev服务器并再次运行它lektor servor,没有出现任何错误

标签的链接在帖子中,但是当我点击并跟随一个链接时,比如说python标签localhost:5000/python,我得到了404 Not Found。我是莱克托的新手。我想知道我做错了什么,我怎样才能让它正常工作


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


更新

我创建了一个github repo包含代码和我到目前为止所做的工作,因此您可以轻松地复制它


更新2

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

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

为每个博客文章的标记生成源路径,但使用root作为父项。 正如你所看到的,我试过:

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

但这不能正常工作

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


Tags: 文档插件localhosturlhtmltagtagsblog
1条回答
网友
1楼 · 发布于 2024-05-23 15:23:34

所以基本上是我做错了,因为我想在tags.ini中使用根作为父对象,就像这样:

    parent = /

最后,我将blog-post.html中的'/blog@tag/' ~ t.lower()表达式更改为如下内容:

    <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 yes之后lekor server一切正常,包括标签系统

相关问题 更多 >