Setting up a static blog with Pelican and Netlify




Have you ever wanted to start your own blog? Are you too lazy to create the whole website from scratch? Or, are you just too busy? Or maybe you haven't found the right kinda motivation yet...

 

Well, I have similar problems, and lucky for me, I found some motivation recently. A couple of days ago, I was reading an article on Full Stack Python, and I found out that it was made using something called Pelican. Now, I knew what Pelican was but I never thought to give it a try until then...

 

So... I finally decided to set-up my own blog using Pelican. Before this, I had my single-page website parked on AWS's free tier but then I decided to move on to Github Pages and Netlify. (Well, my free tier expired too :p)

 

Pelican

 

Assuming you have Python3 and pip installed, start by downloading and installing Pelican. Hit up the console and run:

 

# download pelican library
pip install pelican

 

Note: it's good to use virtual environments but that's for you to decide.

 

Now that Pelican is installed, create a new folder, and in that folder run pelican-quickstart command in terminal. This will create a new Pelican project. After answering to the prompts, a pelican project will be created with all the basic settings in pelicanconf.py file.

 

pelican-quickstart screenshot

 

Type make in the terminal to see the commands you can use to run development server, publish your blog, etc. Alternatively, you can open the Makefile in the project folder, to read, and edit commands.

 

pelican make commands

 

Use make devserver command to launch development server on port 8000 (default). After launching the devserver, check out the website at localhost:8000. It uses default theme for the site, you can create your own theme, or you use this site's theme if you want.

 

pelican demo on localhost:8000

 

 

 

Okay! Now let's write a blog post

 

Create a file with .md extension in [project's root]/content/posts. Add details about the blog post in the header of the markdown file and then write your content.

 

pelican blog example

 

Here: slug is the name of your html file generated, pelican-python.html in this case. Save the file and reload localhost:8000 to find the new blog post there. [no screenshot. see it for yourself :p]

 

 

Okay! Now let's make some changes to pelicanconf.py.

 

# Add following to your pelicanconf.py
# Default date format to use: Eg: (%d %B %Y) => 28 May 2019
DEFAULT_DATE_FORMAT = '%d %B %Y'

# Current year. might be useful
CURRENT_YEAR = time.strftime("%Y")

# Path to theme to be used. I've named my theme basic1
THEME = "themes/basic1"

# List of templates that are used directly to render content. 
# Typically direct templates are used to generate index pages for collections of content 
# (e.g., tags and category index pages). If the tag and category collections are not needed, 
# set DIRECT_TEMPLATES = ('index', 'archives')
# I've add 'blog' here to create a separate page for blog indexing
DIRECT_TEMPLATES = ['index','archives','categories', 'tags', 'blog']

# Auto generate slug from this title, if not specified [Overridden by the Slug: property]
SLUGIFY_SOURCE = 'title'

# How URLs will be created for a blog post. eg - {root}/category/year/month/date/{slug}
ARTICLE_URL = '{category}/{date:%Y}/{date:%m}/{date:%d}/{slug}/'

# How/where to generate HTML file for the blog post. eg - {root}/category/year/month/date/{slug}
ARTICLE_SAVE_AS = '{category}/{date:%Y}/{date:%m}/{date:%d}/{slug}/index.html'

 

Now create blog.html in themes/{theme_name}/templates. This is where you blogs will be indexed. Now you can remove blog indexing from index.html, and make it your landing page. Blogs can now accessed from SITE_URL/blog/. You can refer to theme files for this website's theme to see how I did it.

 

Okay! So now we need to publish our site. We'll use Github Pages and Netlify so host our site. Rasor's Tech Blog has a nice article on how to publish your blog on Github Pages. After you're done with that, we'll link our github pages repository to Netlify.

 

Publishing on Netlify is pretty simple. Signup on Netlify using your Github account and follow the steps. When asked which repository you want to use to serve the pages, select the repository for your blog. Note: if you followed steps from Rasor's Tech Blog article, then when selecting the branch to publish use the pelican branch (or whatever you named it). Done! Netlify will generate a link to your website, and your website must also be published at username.github.io (again if you followed Rasor's Tech Blog). To use your custom domain: It's pretty easy on Netlify. Just follow this link - Netlify - Custom Domains.

 

So, that's it. I hope you liked this post. If I got something wrong, please let me know in the comments below. Also, if you have any thoughts, feel free to share. Have a great day!