PyCharms not detecting Django Templates & TemplateTags directories in apps
I want to enforce that my dev team can't check in any files unless there are no inspection warnings and errors in their python files. However, try as I might, I can't figure out how to get PyCharm to detect Django template and templatetag directories that are in apps, instead of the root project directory. Is there a configuration I am missing or does this feature yet exist? If it doesn't exist, is it possible to contribute to PyCharm Django support?
请先登录再写评论。
Hi Matt,
PyCharm detects templates and templatetags folders in applications, listed in INSTALLED_APPS vairiable from settings.py file, which is stated in corresponding field of Django support settings page.
I actually found that you could right click on a directory and use "Mark Directory As" -> "Template Directory" as well to get PyCharm to pickup template directories. It is definitely not autodetecting template and templatetag directories found in my settings.py. That said, we have a very complicated settings.py that imports settings based on the environment. Does PyCharm only evaluate 'settings.py' as text, or does it perform the imports like python would before evaluating?
Ansered my own question. If your installed_apps is not in the settings.py file, but in an import then it will not be loaded automatically by PyCharm. However, you can specify a different settings file in your django preferences.
This is great! I was wondering the same thing. Now I have a similar question which you undoubtably have figured out too. FWIW I also have a setup.py which not simple. Within my template this is also showing an unresolved path. Any idea how to define STATIC_URL in PyCharm?
<script src="{{STATIC_URL}}js/ajax_select.js" type="text/javascript"></script>
<script src="{{STATIC_URL}}css/ajax_select.css" type="text/css" media="screen, projection"></script>
<link rel="stylesheet" href="{{STATIC_URL}}css/button.css?v=2" type="text/css" media="screen, projection">
I'm afraid I don't have that problem. I may not have that inspection turned on, although I do have the default inspections on. That said I almost never use settings.STATIS_URL in my template, because I have written a templatetag instead:
from django import template
from django.conf import settings
register = template.Library()
class MediaUrlNode(template.Node):
"""Template node that renders the media_url tag."""
def __init__(self, path_params):
self.path_param_vars = map(lambda x: template.Variable(x), path_params)
def render(self, context):
final_path = ''.join(map(lambda x: x.resolve(context), self.path_param_vars))
return '%s%s?v=%s' % (settings.STATIC_URL,
final_path,
getattr(settings, 'MEDIA_HASH', '0'))
@register.tag
def media_url(parser, token):
"""Tag for cache static resource urls. Usage is simply {% media 'css/base.css' %} or for any other similar static
resource; js/images and such."""
try:
path_params = token.split_contents()[1:]
except ValueError:
raise template.TemplateSyntaxError, \
"%r tag requires exactly at least one argument" % token.contents.split()[0]
return MediaUrlNode(path_params)
We do continuous deployment and had problems with cached images, so this template tag also references a value MEDIA_HASH, which is that last git commit hash. However, you could remove that part and use the above code snippet instead of settings.STATIC_URL to avoid the PyCharm inspection error.
You can have another problem: I always name my application "app" (so it's on top in the project folders listing). So PyCharm has created the file "apps.py" in the folder "app", and added in settings.py ,
So the problem is in the file apps.py:
You have AppConfig twice!! So it's never user. Solution: rename, for example, to MyConfig:
And dont forget to change the settings.py accordingly: