Posted on 2020-05-13T09:42:16Z by Kevin Foong
Slugs are great for SEO and humans alike. For example, "/post_detail/vue-spa-and-flask-together" looks much better than "/post_detail/8".
In Python it is very easy to create slugs from the title of your blog post. Just use the excellent python-slugify library. The command is simple.
slugify("Vue Spa and Flask Together")
This will give you the slug "vue-spa-and-flask-together"
Now in order to use the slug you will need to store it in your database. As you will be using the slug to query the database instead of the post id, you will also need to ensure that the slug is unique before saving to your database.
For the full source code of how this can be done view my Github page.
Posted on 2020-05-13T09:26:47Z by Kevin Foong
Sitemaps are handy to let search engines know which pages they should crawl on your website. There is a way to automatically create a sitemap.xml route for your Flask website which will always reflect the latest pages on your site. That is everytime you add a route, it will automatically reflect in your sitemap.xml
You can see this site's dynamic sitemap at https://www.kevin7.net/sitemap.xml
The full code is shown below. The key points are:
- Create a route as per normal and name the route "sitemap.xml"
- To get all the static routes just read in the app's "url_map" method via the "iter_rules" method and include any route that is a HTTP "GET" method.
- For the static routes you can just use an arbitary sitemap last modified date to be 10 days prior to today.
- To get all the dynamic routes (blog postings, etc) you will need to connect to your database, read in each blog posting, and manually build your routes. If your blog post has a timestamp in the database, then use that for the sitemap last modified date.
- Finally render sitemap.xml via a template. Make sure you specify the header to be "application/xml" or else it won't render correctly.
.. more ..