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:
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:
But what about in a Django template? Let’s say I want to use apnumber
from django.contrib.humanize
. You also get a 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:
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
:
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:
It needs a {% load %}
tag, and Alt-Enter
works here as well:
Same applies here: let PyCharm do the work for you, saving you a context switch.