ChatGPT解决这个技术问题 Extra ChatGPT

Does PyCharm support Jinja2?

A bottle project of mine uses Jinja2. PyCharm does not automatically recognize it and shows such lines as errors. Is there a way to make Jinja2 work?


k
k0pernikus

In the pro edition, these template languages:

Jinja2

Django

Mako

are supported. You can configure the template language in the project's settings:

https://i.stack.imgur.com/TS8P4.png

The community edition may lack certain template languages.


I got "Expected }}" warning for every function calls such as {{ func('hello word') }}. How can I fix it?
You need to apply the change, otherwise it will go back to assuming django formatting.
"Out of the box" is not correct for PyCharm Community edition -- see below. (I tried to edit this information into the answer, but it got rejected 3-2.)
@Noumenon I've added the information to my own answer, as I think that clarifies it a bit more. The reject was, in principle, fine by stackoverflow standard (changes intent of the author) and edits are not necessarily there to correct false answers, for that one should either downvote the answer and create your own and write a comment. Yours was an edge-case as I do think it would have improved my answer significantly.
Your comment would have made a great, educational rejection message for my edit. "Doesn't improve the answer even a little" tends to get my hackles up. Thank you.
S
Stan Prokop

I think it's worth to mention that PyCharm Community edition does not support Jinja2, Mako and Django. It's available only in PyCharm Professional.

See comparison of the two.


Also worth to mention that (even as of PyCharm 3.1) Debugging Jinja and Mako templates is not supported [even in pro edition]. jetbrains.com/pycharm/webhelp/debugging-django-templates.html
This is very worth mentioning. I just spent 5 minutes looking through the project settings (based on the first answer) before I read this answer.
A
Ayyoub

https://i.stack.imgur.com/7X7lU.png

From File open Settings and search for python template under Languages & Frameworks Select Python Template Languages from there Click HTML And Select Jinja2 as Template Language.

please see the image for better understanding.


I am a bit confused as to where the difference is from my answer: stackoverflow.com/questions/15750551/…
@k0pernikus this is the dark theme answer :)
This has newbie-friendly steps, plus one for that!
This answer is the one that helped me, because of newbie friendly steps.
Thanks a bunch!
T
Trilarion

If you are using .jinja extension instead of .jinja2, it won't work, templates are not highlighted.

You have to add the file extension to the filetypes section.

Preferences > General > Filetypes

Scroll to Jinja 2 Template

Register new pattern by clicking +, add *.jinja

https://i.stack.imgur.com/HyYEX.png


Thanks,it's helps me a lot
I use the jinja2 extension but this doesn't seem to play along with the template settings that only allow to pick html. Should I use html or jinja2 as extensions in order for the preview to work?
c
charu dhingra

In community edition, the python template option is not available, so you can simply click on python packages next to the terminal present on the bottom. This will also add Jinja2