How can I set up a new comment string in PyCharm for DTL code in .html file?

In PyCharm we have a convinient way to turn off selected part of a code by pressing Ctrl+/. Later we can turn it on back by pressing the same key combination.

However if I am inside the HTML code the combination Ctrl+/ generates comments <!--some error code here-->. It works well for HTML. But for Dynamic Template Language (DTL) from Django the comments should be as follows: {% some error code here %}.

Is there any way to set up the key combination Ctrl+/ in PyCharm for making the DTL comments i.e. {% some error code here %}?

I use PyCharm 2023.1.2 (Community Edition) and Django 5.2.10.

The next solution is work well only for one separate string of code. And does not work with block of code.

We have to write a macro and then assing a combination of keys for it:

Start macro recording

Edit > Macros > Start Macro Recording

Press Home to put cursor at the beginning of the string

Then type {#

Press End to put cursor at the end of the string

Type #}

Stop macro recording:

Edit > Macros > Stop Macro Recording

Confirm the code of macro

Edit > Macros > Edit Macros

enter image description here

Name it e.g., DTL_comment

Then assign key coombination

Ctrl+Alt+S > Keymap > Macros > Invoke Macro >Right Click > Add Keyboard Shortcut > Ctr+Shift+Q

Press Apply, OK

So, put cursor on the string you want to comment with {# and #}

and then press Ctr+Shift+Q.

Mark .html files as Django Templates

  1. Settings → Editor → File Types

  2. Select Django Template

  3. Add *.html (or ensure it’s already there)

Restart PyCharm after this if things feel stuck.

Вернуться на верх