13

I do not think this is a duplicate. I have looked at a lot of answers to similar problems.

Please note: This runs perfectly fine and django finds all static files.

UPDATE: It looks like this is a PyCharm bug. If I move static into my app directory, the PyCharm can resolve it. But, I have this static in the main src dir because multiple apps will be using the CSS. Right now it runs and Django has no problem with this. I am thiking this is a PyCharm bug.

PyCharm is set up, and everything else seems to work as far as the Django project goodies that Pycharm offers.

Pycharm cannot resolve, whch means that I cannot use auto complete, and it is annoying. If I hit ctrl-space the only directory that pops up is admin. This means that PyCharm is completely ignoring my static directory.

I have also tried making it a templates directory and a resourse director. No luck there either.

{% load static %} <!-- {% load staticfiles %} -->
...
<link rel="stylesheet" href="{% static ''%}">
<link rel="stylesheet" href="{% static 'css/skeleton.css' %}">
<link rel="stylesheet" href="{% static 'css/custom.css' %}">
...

The code above works fine. PyCharm does not find the urls though.

INSTALLED_APPS = [
    'django.contrib.staticfiles',

    # admin specific
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.messages',
    'django.contrib.sessions',
]
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static')
]

STATIC_URL = '/static/'

Michael Bruce
  • 10,567
  • 2
  • 23
  • 31
  • Have you properly set PyCharm's `project structure` setting, and its django settings, esp. project root folder? – user2390182 Feb 10 '17 at 14:57
  • As far as I know, the only thing to set up is shown in the pictuer above. – Michael Bruce Feb 10 '17 at 16:01
  • after static tag {{ STATIC_URL }} hit ctrl + space to find available folders, do the same until you get to desired file. Also don't forget to run collectstatic when you make changes – Josh Apr 05 '18 at 12:09
  • A bit late, but..i had the very same issue with Flask. Setting up the template language as "jinja2" in settings' page `Languages & Frameworks \ Template Language`, the error disappeared and the file was correctly recognized. Maybe there's a setting for Django's templates, too. – Erenor Paz Apr 19 '20 at 13:50

7 Answers7

11

Here is the key to solving the problem: Pycharm needs to know where your settings files that you're currently using is located. See the picture.

enter image description here

Also make sure that you have everything setup in your settings file correctly. See the picture.

enter image description here

Mohammad Mahjoub
  • 417
  • 4
  • 12
  • For PyCharm v2021.2: If you start a Django project this setting will be correctly interpreted. It looks like this issue was resolved (or the OP's project was not setup as a Django project). – Carewen Oct 12 '22 at 18:08
2

In some situation this way can work:

<link href="{{ STATIC_URL }}" rel="stylesheet" type="text/css">

PyCharm static autocomplete

trianglesis
  • 83
  • 1
  • 2
  • 13
  • Additional option: use string: STATICFILES_DIRS = ('static', os.path.join('static', 'admin'),) - my previous setup had: STATIC_ROOT = os.path.join(BASE_DIR, 'static') STATICFILES_DIRS = (os.path.join(STATIC_ROOT, 'admin'),) – trianglesis Jun 19 '19 at 16:08
1

If you have setup everything correctly according to Django doc at: https://docs.djangoproject.com/en/3.1/howto/static-files/

Then you can try to explicitly set your static folders to help the IDE like:

STATICFILES_DIRS = (
    BASE_DIR / "app1/static",
    BASE_DIR / "app2/static",
)

this works for me many times. IntelliJ just could not automatically find my app's static.

Minh Thai
  • 568
  • 6
  • 18
0

PyCharm recognises my static files for a configured settings.STATIC_ROOT as below

STATIC_URL = '/assets/'

STATIC_ROOT = os.path.join(BASE_DIR, 'static')
All Іѕ Vаиітy
  • 24,861
  • 16
  • 87
  • 111
  • "I have also tried making it a templates directory and a resourse director. No luck there either." - I mentioned that I have done this already. And yes, it is set up as you have it. – Michael Bruce Feb 10 '17 at 19:26
0

Try loading your staticfiles:

{% load staticfiles %}

And setting your link as:

<link rel="stylesheet" type="text/css" href="{% static "css/default.css" %}">
Josh
  • 2,122
  • 1
  • 21
  • 28
0

I used:

  1. STATIC_ROOT = os.path.join(BASE_DIR, "static_deployment")

  2. python manage.py collectstatic command

So I did what needs to be done to go from development to deployment configuration.

pyCharm now works fine and can see static files

Yes it is not the best solution if you change static files a lot but can help developing with pyCharm and Django...

Adelin
  • 7,809
  • 5
  • 37
  • 65
Petr
  • 1
-2

Check in settins.py

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'your_app/static/')
]

and add

STATIC_ROOT = os.path.join(BASE_DIR, '/static/')

Pycharm use this settings.