Focus on the Article class. You’ve defined the title of your article and that’s it. It is there now. Unless you change it manually by yourself, it won’t change. In other words, it is static. And it doesn’t help when you get a lot of data and need to show it all on your website / app.
This is where Django model tags come in handy. Consider, for example, that you are using an API that retrieves post titles and their links, and you want your website to display all of them, old ones as well as those that you retrieve on each refresh. Using the template tags, we can display the article title dynamically. Here’s how, focus on 3 lines that don’t seem like HTML i.e. template tags.
Note on – for item in all_headlines
item is local, you can set it as you want. And all_headlines comes from the views.py file where you can set it as shown below, where the title is a model, and I retrieve a total of 25 articles, the last article published first. Using a hyphen (-) gives us the reverse order.
But however, let’s get back to our main topic. As you can see, I am getting links to all of the articles, but there will be a hyperlink to “link”. I don’t want that. I need a domain name as shown in picture 1 so that users know where these articles are coming from. Now, this is where we end up in a stalemate, isn’t it?
Django model tags are limited to what it can do. It is not a foolproof programming language that supports multiple packages. But that shouldn’t stop you. And luckily there is a way. There always is.
We have explained why we need custom functions, let’s move on to How? ‘Or’ What. How to create custom functions? How do we use them to do what we need from them? And maybe How do we make our life a little easier? No, that’s not true, there is no such thing as an easy life. Let’s move on.
To make sense and facilitate your understanding, I will explain the solution to a problem I was facing. Using custom functions to extract domain names from URLs and print them dynamically instead of just the hyperlink placeholder “link”.
There are 4 things you need to do to use Custom Functions. Let’s go through it one by one.
- Create a new folder in your application and in this folder create a new .py file. The python file is your playground. You can define it however you like. See the structure below,
main/
|--project/
| |--app/
| | |--migrations/
| | |--templatetags/ <- templatetags folder
| | | +-- replace.py <- python script
Here is the Python script I wrote to replace URLs with domain names using regular expression. I sometimes wonder what the regular expression can’t do.
2. Load this .py file into the main HTML file
Now you need to load the replacement file into the html file. You can do this by typing the following.
{% load replace %}
3. And finally, use the custom function
Remember the second figure where we used template tags for dynamic content. We just need to change a minor thing to use our custom function. Since we need a domain name from a url, we’re going to use the block that fetched the urls and then replace it with the new string which is just the domain name, all dynamically. .
Do you notice anything different? That’s right, we just have to use our custom function by typing the following.
{{ article.url | replace }}
And that’s all. This is how you do it
The sole purpose of writing this article was to give you all the information you need regarding custom function in one place, as I had to look at several different places and spend a few frustrating nights to achieve it. Hope this helps you. However, if there is another way to implement custom tags or improve efficiency, I would love to hear from you.
You can always leave feedback or contact me on Twitter for any questions, or just go digital to say hello. I always love connecting great people. Good learning.