I'm new to PyCharm, and I'm trying to use it for Django development. My app is structured like this:
bs3app/ ├── __init__.py ├── templates │ └── home.html ├── urls.py └── views.py
In bs3app/views.py
, I get a warning:
Template file 'home.html' not found
The source code:
from django.shortcuts import render_to_response def home(request): return render_to_response('home.html')
The template file is there in bs3app/templates/home.html
. The home
view works correctly when I run the site. On the Django Support page of the project, Enable Django Support is on, and the Django project root, Settings and Manage script values are all correct.
So, why am I getting the warning? As per this PyCharm doc page, I can mark the template directories explicitly and then the warning goes away, but why do I have to? Why can't PyCharm figure them out automatically, given the settings.py
file of the project?
The project is on GitHub, if you need to see other files.
Press Ctrl+Alt+S to open the IDE settings and select Editor | File and Code Templates. and specify the template name, file extension, name of the resulting file, and body of the template. Apply the changes and close the dialog.
Configure a template language for a projectOpen the Settings/Preferences dialog, and click the node Python Template Languages. In the Template Languages page, do the following: From the Template language list, select the specific template language to be used in project.
The Python string Template is created by passing the template string to its constructor. It supports $-based substitutions. This class has 2 key methods: substitute(mapping, **kwds): This method performs substitutions using a dictionary with a process similar to key-based mapping objects.
TL;DR Goto Settings | File Templates switch to Includes tab, create a new file and type e.g. #set( $MyName = "John Smith" ) . Then go to your template in the Templates tab and add to the template on the top #parse ("your-include-name. your-extension") Now you can use $MyName in this template.
Just open the project view
(view->tool windows -> project). There right-click on your templates
-folder -> 'mark directory as'-> Template directory
Try adding TEMPLATE_LOADERS
to your settings file if you don't have it. I think PyCharm looks for it and doesn't load it from Django default settings. It solved my problem.
settings.py:
TEMPLATE_LOADERS = ( 'django.template.loaders.filesystem.Loader', 'django.template.loaders.app_directories.Loader', )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With