In this video, we will explore the possibility of creating custom filters to make frequently used behaviors easily reusable for everyone on our team.
- [Narrator] In this section we'll discuss custom template tags and filters. We'll start out with filters since they're simpler. Then we'll cover template tags. You've probably used filters in your templates. One that people often try out at first is upper. We can see how it works here. We display a variable, but then we add a pipe, and then at the end the name of the filter. Some filters can take a parameter. For example, the date filter takes a string that describes a date format.
In this case, you'd use the pipe, the filter name, and then a colon and the parameter in quotes. Like this. In order to create your own filters or template tags, your app has to have a module called templatetags. And yes it does need the __init__.py file. If you have a set of filters and template tags that you use on multiple projects, you can create a Django app just for these template tags. In that case, the app still needs to be installed in the settings.py file.
What you name your files inside the templatetags folder doesn't matter. In this case, I'll put my filters inside of a file called filters.py. However, it's no problem at all if I called this file fred.py. Likewise, I can have multiple files for my filters so that related filters are in the same one. In my filter module, I need to import the template module from Django. This will allow me to use the register decorator. To create a filter, I just define a function that takes one or two values, and then I decorate it with register.filter.
In these cases I'm also specifying that the is_safe property is true to let Django know that I've already checked my strings to ensure they're safe. I'm actually lying because I haven't checked publisher to see if it's clean. In your own projects, be sure to check the HTML your filter returns to ensure it's clean. If you're not sure that your mark up is safe, set it to false and Django will escape potentially unsafe characters. You can learn more about this in the custom template tags documentation. The first filter doesn't take a parameter. It simply takes a variable that my template passes in and then assigns it to the value publisher.
My filter is called publisher_link. So to use it in my template, I would add the pipe symbol followed by publisher_link. The value of publisher will be passed into my function. Before this will work though, I need to load the filters. The reason I'm saying load filters here is because I named my file filters.py. If I had named it fred.py, then instead I would say load fred.
My second filter is called youtube_embed, and it does accept a parameter. The url will be passed in as the variable, and if the embed should be HD, then I'll pass in true with a colon. I think you see now that this is not rocket science. The main issue that I run into is that I forget to create a module called templatetags, and sometimes I forget to add my app to the installed apps in settings.py. I make one of these two mistakes pretty much every time.
Once I do remember to add the app, everything works fine. In the next video, let's create template tags.
Django is one of the world's leading web development platforms. This Python web framework makes your app building experience smoother, faster, and easier. It is a key feature behind the success of many start-ups across the world, including Instagram and Nextdoor. This course introduces next-level advanced concepts to help you build your own professional-grade Django applications.
Author Kevin Veroneau starts off by explaining how class-based views—a powerful, but often poorly understood feature—work in Django. Then learn how to create custom middleware to help organize frequently used code. Have you ever wanted to build a complete RESTful API? Find out how to do just that, starting with a basic read-only API and then adding key-based authentication to allow the full range of CRUD functionality. Plus, discover how to use model managers and the Q method to make it easier to get the data we need to our views and clients.
For the finishing touches, dive into the details of caching to improve application performance, and then add custom management commands to help automate routine tasks.
- Extending class-based views
- Creating custom class-based views
- Managing your database with middleware
- Creating template filters and tags
- Building a RESTful API
- Modeling advanced database relationships and queries
- Using a model manager for your application