简体   繁体   中英

Pycharm + Django 1.3 + STATIC_URL in templates = Unresolved static reference

PyCharm (1.3 and 2 beta) in my Django 1.3 project throws a lot of "unresolved static reference" errors when inspecting my templates for script and style includes.

In an outdated PyCharm doc , I found that a small guide that doesn't work in my situation, because my static files are spread over multiple apps. Adding my static dirs to STATICFILES_DIRS also didn't work.

Dir structure (simplified):
  app1/static/js/file.js
  app1/static/css/file.css
  app2/static/js/otherfile.js
  app2/static/css/otherfile.css
  templates/template.html

Template.html:
  <script src="{{ STATIC_URL }}js/file.js"></script>

file.js resolves when I visit the template on localhost, but not in PyCharm.

How do I make static files resolve in PyCharm?

Go to Settings in Pycharm 2.73

Settings >> Project Setting >> Django

Enable the Django support and provide the paths for the three following files:

Project Root
Settings file
Manage.py file

When you have given these informations, close PyCharm and restart it.

PyCharm 2.5 finds my static files again.

The trick is to mark app1/static and app2/static as "Source Root".

STATICFILES_DIRS is not working for me.

The selected answer doesn't work for me. What solved it is using a prefix in STATICFILES_DIRS:

STATICFILES_DIRS = (
    # ...
    ("resources", "C:/data/django/myproject/myapp/static"), )

as documented in the django docs: https://docs.djangoproject.com/en/1.4/ref/contrib/staticfiles/

Then in your html template:

<link rel="stylesheet" href="{%static 'resources/favicon.png' %}" type="text/css">

STATICFILES_DIRS works for

{% static "js/lib/jquery-1.8.2.min.js" %}

tag in template. Not for {{ STATIC_URL }}js/lib/jquery-1.8.2.min.js

http://youtrack.jetbrains.com/issue/PY-5568

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM