问题描述
我正在尝试使用 lektor CMS
构建一个博客,为此..我需要一个标签系统,搜索后我在 lektor 上找到了一个 lektor 插件docs 称为 lektor-tags
我遵循了文档中的每一步,挣扎了很多,甚至还访问了 github repo 以查看文档中是否还有其他内容。
我的问题是当我尝试访问 localhost:5000/{the_tag_name}
时说 localhost:5000/python
我总是得到一个 404 Not Found
说
在服务器上找不到请求的 URL。如果您手动输入网址,请检查拼写并重试。
这是我目前所做的:
之前使用lektor-tags
:
-
为
models/blog.ini
中的[children]
添加了 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 个帖子,一切正常。
此时我想使用标签系统,所以我选择使用lektor-tags
,我所做的是:
-
安装
lektor plugins add lektor-tags
-
使用此配置创建
configs/tags.ini
:parent = / url_path = {{ this.parent.url_path }}{{ tag }} tags_field = tags ignore_missing = true template = tags.html
-
使用以下内容创建了
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 %}
-
编辑了
models/blog-post.ini
并添加了:[fields.tags] type = strings
-
在
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 %}
-
最后我更新了一个帖子以包含来自管理员的一些标签,并确保它在该帖子的
content.lr
中。所以我停止了 lektor 开发服务器并再次运行它lektor servor
,没有出现错误。
标签的链接在帖子中,但是当我点击并点击链接时,例如 python 标签 localhost:5000/python
我得到 404 Not Found
。我是 lektor 的新手。我想知道我做错了什么,我怎样才能让它正常工作?
注意:我使用的其他插件是 lektor-minify
,lektor-disqus-comments
这些插件的文档很简单,我没有感到困惑,但是当涉及到这个特定的插件我很困惑,挣扎:文档不是那么好解释,我感觉完全迷失了!
更新
我创建了一个 github repo,其中包含代码和我目前所做的一切,以便您可以轻松地复制它。
更新 2
我设法让它正常工作,请参阅下面的答案,但是现在我想知道如何将根设置为父级,换句话说,如何编辑此表达式:
<a href="{{ ('/posts@tag/' ~ t.lower())|url }}">
为每个博客文章的标签生成一个源路径,但使用 root 作为父级。 正如你所看到的,我试过这个:
<a href="{{ ('/' ~ t.lower())|url }}">
但这不能正常工作。
值得一提的是,lektor 使用 jinja2
模板语言。
解决方法
所以基本上是我做错了,因为我想像这样使用根作为 tags.ini
中的父:
parent = /
我最终将 '/blog@tag/' ~ t.lower()
中的表达式 blog-post.html
更改为类似:
<a href="{{ ('/' ~ t.lower())|url }}">
使其无法为每个博客文章的标签生成源路径
我改变和工作的是:
-
我选择了
posts
作为父级,将configs/tags.ini
更新为:parent = /posts url_path = {{ this.parent.url_path }}{{ tag }} tags_field = tags ignore_missing = true template = tags.html
-
更新了
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
一切正常,包括标签系统。