Tips & Tricks Tutorials

Django Custom Tags in PyCharm Professional 2020.1

PyCharm started its life with Django support and our new release adds some useful improvements for Django coding. Let’s take a look at support for custom Django tags in PyCharm Professional 2020.1.

But First, One More Thing

Maybe it’s bragging, but we need to show one existing feature related to this topic.

When you’re in some general Python code and you need something imported, PyCharm will give you visual cue — a warning — as advanced notice:

Python Import Warning

To fix it, Alt-Enter can do it for you. No need to stop what you’re doing, go to the top, type in the import, then find your way back:

Python Generate Import Gif

But what about in a Django template? Let’s say I want to use apnumber from django.contrib.humanize. You also get a warning:

Builtin Filter Warning

This requires a {% load apnumber %} tag at the top of the template. In 2020.1, PyCharm’s Alt-Enter will do the janitorial work for you:

Builtin Filter Warning Gif

One more way we try to help keep you in the flow.

Load My Stuff Too

That’s great for bundled tags and filters in INSTALLED_APPS. What about my custom apps?

For example, in my app’s templatetags directory I have a module with a new filter named cut_all:

Cut All

We’d like to use this filter in a Django template, for example, the same spot as the above usage of a bundled filter. When we type in cut_all, PyCharm helpfully gives us a warning:

cut_all_warning

It needs a {% load %} tag, and Alt-Enter works here as well:

Generate Custom Load

Same applies here: let PyCharm do the work for you, saving you a context switch.

image description