PyCharm 2018.3.2 reads Django's 'APP_DIRS' setting in TEMPLATES wrong, causing really bizarre behavior

I've been fighting with PyCharm for the last several weeks, trying to figure out why it was refusing to detect my apps' templates folders. It had worked in the past, but things changed recently (Perhaps with 2018.3? I'm not certain). Now, if APP_DIRS is set to anything except True in the initial definition of the Django TEMPLATES setting, PyCharm can't find my templates folders. Even setting it to True after the TEMPLATES = ['stuff'] line in settings.py doesn't fix the folder finding, which is really bizarre.

Here's a screenshot showing the folder detection actually working. Note that APP_DIRS is set to True in the definition of TEMPLATES:

Unfortunately, I can't use APP_DIRS = True in production, because it's slow and uncached. We use Django's cached template loader, like in this screenshot, which breaks the folder detection:

I've also tried using the uncached loader, but that's equally ineffective.

The REALLY weird thing is that setting APP_DIRS = False in the definition of TEMPLATES, and then setting it to True in the next line of settings.py still breaks the folder detection!

WTF?? How is that even possible? Is PyCharm, like, parsing the contents of settings.py to look for TEMPLATES = [ ... 'APP_DIRS': True ... ] or something?? I mean, it can't really be that, either, since I defined TEMPLATES in django.py, which is imported by settings.py. So what the heck is going on??

Please help me figure this out, as my brain is totally melted after all this frustration.

5 comments

Hi,

I've tried to reproduce your issue, but unfortunately I couldn't. I've changed APP_DIRS to a specific path instead of True, but PyCham still shows my template dirs in the structure list.

Can you provide example of the whole TEMPLATES object from your settings.py? Alternatively, if you could reproduce the issue with a test project and attach this project here, that would be most helpful.

0

APP_DIRS is just a boolean, so "setting it to a different path" wouldn't have had any effect, because a string is a truthy value. Perhaps you're confusing APP_DIRS with DIRS?

The TEMPLATES object in my settings.py is fully visible in the bottom of each of the screenshots I provided. I made sure to make it visible to show exactly what its value was in each behavior case.

 

I've created a barebones Django project in a PyCharm project that reproduces the bug: https://github.com/coredumperror/pycharm_test

To create this repo, I started a new, empty PyCharm project, then inside that project I used the instructions at https://docs.djangoproject.com/en/2.1/intro/tutorial01/ to create a fresh, default Django project (test_project) with a single default app (test_app). I then added a view function in test_app/views.py, and a template, test_app/templates/test_app/index.html.

At this point, PyCharm correctly detected the templates folder. I could Cmd-click on 'test_app/index.html' on Line 5 of test_app/views.py, and PyCharm would open index.html.

I then edited test_project/settings.py to change the initial definition of TEMPLATES so that APP_DIRS is False, then added another line after the definition of TEMPLATES that sets APP_DIRS back to True.

At this point, PyCharm could no longer detect my templates folder, and it showed an error about not being able to find 'test_app/index.html' when editing test_app/views.py.

I hope this is enough to help you track down and fix this bug. It is very annoying. If you need anything else, please let me know.

0

I can confirm this behavior - setting APP_DIRS using constructs like "TEMPLATES[0]['APP_DIRS'] = True" doesn't work, but if you re-define the whole TEMPLATES object like this (see the following snippet http://bit.ly/2SUu14H) it works again.

This maybe related to specific ways PyCharm parses Django settings, but normally this shouldn't be a problem. Is there a reason why you want to define APP_DIRS more than once ? You've mentioned that you're using cached templates loader due to performance issue, but how is this related to setting APP_DIRS more than once?

I'm trying to understand your use case to see if it warrants any fixes on our side. 

0

Heh, I wrote up this big, complex reply that explained a bunch of things about why I had to define TEMPLATES in the way I did. But in order to do due diligence, I also double-checked the Django template system docs to confirm something... only to realize that I'd been doing my TEMPLATES settings in a dumb way this whole time. lol

It turns out that it's totally possible to define APP_DIRS as True, and leave it set to True, while still enabling template caching. As long as TEMPLATES['OPTIONS']['debug'] = False, Django will perform template caching for you.

Once I set up my code to set 'debug' to False in production, and True in development (to disable caching while we're actually editing templates), I was able to leave APP_DIRS = True, which means PyCharm can successfully detect my template folders!

So, I no longer have a do-or-die issue with PyCharm's template folder detection. However, I do think PyCharm's mechanism for determining where to look for templates needs some work, due to that odd behavior we both confirmed in regards to how it reads APP_DIRS. But since the problem really only pops up when you're messing with the 'loaders' setting, which the vast majority of django projects don't actually need to mess with, this issue with PyCharm is not a high priority.

0

Glad to hear it's working for you. Thanks for the additional research done on your part.

The algorithm used for reading the settings might be just not accounting for some non-standard use cases, or workarounds implemented by users. To help developers come up with the best solution, we need a valid real-life case. We will definitely keep this issue in mind and gather more feedback from other users for further analysis to improve the overall integration with Django.

If you'll have any other issues in the future, don't hesitate to contact JetBrains support team.

0

Please sign in to leave a comment.