My Personal Blog

Home

Adding Markdown Support to the Blog

Jan. 19, 2024 | Categories: dev

Adding markdown was relatively straightforward thanks to this tutorial. All I had to do was to import the markdown package in the views.py file and convert the post.body to markdown.


import markdown 
# ...
def blog_detail(request, pk):
md = markdown.Markdown(extensions=["fenced_code"])
post = Post.objects.get(pk=pk)
post.body = md.convert(post.body)
# ...

I also had to modify the HTML template to flag the markdown to be rendered as safe:


<p>{{ post.body|safe | linebreaks }}</p>

For some reason the markdown is still pretty ugly, so I'll have to see if this could be fixed by a template.


While the change to the code was relatively simple, I struggled a bit with figuring out what the local development workflow should be. I ran into two issues.



  1. I am using poetry to manage the environment, and for some reason Django was not installed, so I had to reinstall everything with poetry install --no-root. The --no-root was due to the install erroring out when trying to install the project. I'm not sure if that is a problem but it ended up working fine.

  2. When I tried poetry run ./manage.py runserver, I got an error relating to the database. I realized this was due to some changes I had made to the settings.py file for deploying on Render. Notably, this entailed switching away from a SQLite db to postgres. I therefore had to figure out how to get postgres running (I downloaded the mac app, and ran postgres=# CREATE DATABASE personal_blog; to get the db I needed.)


So far this has been decent progress. I am realizing that:


Leave a comment:

Comments:

On Jan. 19, 2024 Adrien wrote:

The linebreaks are massive. Why?

On Feb. 19, 2025 Celesta wrote:

https://jekyll.s3.us-east-005.backblazeb2.com/20241213-1/research/je-tall-sf-marketing-(111).html
Knowing a little bit about what you’d wish to put on might help you narrow down your choices.