Search code examples
pythontagsblogslektorstatic-site-generation

Lektor CMS : Can't get lektor-tags to work properly, The requested URL was not found on the server


I am trying to build a blog using lektor CMS, for that.. i needed a tags system, after searching i found a lektor plugin on lektor docs called lektor-tags

I followed every step on the docs, struggled a lot and even visited the github repo to see if is there anything else not included in the docs.

My problem is when i try to visit localhost:5000/{the_tag_name} say like localhost:5000/python i always get a 404 Not Found saying that

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

This is what i did so far:

Before using lektor-tags:

  1. I changed the route for the blog posts to be /posts instead of /blog.

  2. Added slug format to models/blog.ini in [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. Created 3 posts, everything was working properly.

At this point i wanted to use a tags system so i chosen to use lektor-tags, what i did was:

  1. Installation

    lektor plugins add lektor-tags
    
  2. Created configs/tags.ini with this config:

    parent = /
    url_path = {{ this.parent.url_path }}{{ tag }}
    tags_field = tags
    ignore_missing = true
    template = tags.html
    
  3. Created templates/tags.html with the following content:

        {% 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. Edited models/blog-post.ini and added:

       [fields.tags]
       type = strings
    
  5. In templates/blog-post.html i added the following to show links to the page that contain a list of all posts with the specific tag:

        {% 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. Finally i updated a post to contain some tags from the admin and made sure it's there in content.lr for that post. so i stopped the lektor dev server and run it again lektor servor with no errors to be present.

The links were there in the post for the tags but when i click and follow a link say for example for the python tag localhost:5000/python i get 404 Not Found. i am new to lektor. i'm wondering what i did wrong and how can i get this to work properly?


NB: Other plugins i used are lektor-minify, lektor-disqus-comments the docs for these plugins were straightforward and i didn't got confused, but when it comes to this specific plugin i were confused, struggling: the docs were not that great and explaining, i felt completely lost!


Update

I created a github repo containing the code and what i have done so far so you can easily replicate this.


Update 2

I managed to get this to work properly see my answer below, however now i'm wondering how to set the root as the parent in other words how to edit this expression :

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

to generate a source path for each of the blog post's tags but using root as the parent. As you can see i tried this :

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

but this doesn't work properly.

It's worth mentioning that lektor uses jinja2 templating language.


Solution

  • So Basically it was me doing this wrong because i wanted to use the root as the parent in tags.ini like so :

        parent = /
    

    I ended up changing the expression '/blog@tag/' ~ t.lower() in blog-post.html to something like :

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

    making it not able to generate a source path for each of the blog post's tags

    What i changed and worked was :

    1. I chosen posts to be the parent, updated the configs/tags.ini to :

      parent = /posts
      url_path = {{ this.parent.url_path }}{{ tag }}
      tags_field = tags
      ignore_missing = true
      template = tags.html
      
    2. Updated 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 %}
      

    After running lektor clean --yes then lekor server everything was working properly including the tags system.