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