Django example: creating a custom block template tag

Toys lined up

Sometimes while you’re building a site using Django you may notice that you’re repeating the same chunk of HTML over and over again on different pages. Depending on the complexity of the HTML, that could create a maintenance nightmare. In this example I’m going to show how you can wrap up that HTML block and reuse it without repeating it using a custom block template tag.

The goal in this example is to build a sort of sub-template that can be reused on any template page. In some cases it may be sufficient to have a snippet that is added to another template using the {% include %} block, but since what you can pass into that block is so limited we’ll sometimes need to roll our own.

Toys lined up
Template tags can be reused in different contexts… much like these toys.
Image credit: Vanessa Bucceri via Unsplash.

Block tags differ from regular template tags because in order to invoke them you have to use at least 2 tags (a beginning and an end) and any code you put between them can be interpreted and altered by the tags. You’re probably familiar with block tags such as {% block %}{% endblock %}, {% for %}{% endfor %}, and {% if %}{% endif %}.

In this example we’ll create a template tag that will auto-generate all of the HTML necessary to display a Bootstrap modal using custom template block tags {% modal %} and {% endmodal %}.

Continue reading “Django example: creating a custom block template tag”

How I broke my Facebook addiction (without going off the grid)

So I was addicted to Facebook. Like, really addicted. While at work, anytime I found myself stuck on a problem I’d immediately open a new browser tab just to zone out by reading the latest posts from friends and from any of the dozen groups I had subscribed to. I was living in a cycle of shallow, getting easily overwhelmed by my work and taking quick escapes before I had to think too hard about anything. I was like a rat with a button – just going to Facebook was giving me small dopamine hits, so I kept going and kept going.

I was also addicted at home. Facebook was always the most used app on my phone. I got on immediately when I woke up in the morning, I’d scroll as I walked my dog three times a day, and I’d zone out while I ate dinner. Hardly an hour would go by in my life when I didn’t check Facebook at least once.

laptop-mobile.jpg

I knew something had to change. I was addicted. But at least I had taken the first step: admitting I had a problem. Now I just had to do something about it.

My goal was not to complete quit Facebook, nor did I want to go cold turkey. My only goal was to stop turning to Facebook any time I was feeling an emotion. Instead, I wanted to feel the emotion, to experience discomfort, push through it and grow from it.

Continue reading “How I broke my Facebook addiction (without going off the grid)”