Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
299 views
in Technique[技术] by (71.8m points)

python - Django: static file not found

Running Debian on Virtual Machine guest inside Windows host. Set Bridged for adapter-type. Installed Django on the guest and using build-in runserver and built-in database for testing purposes.

Having simple files structure:

> ..
> templates
>   base.html
> static
>   css
>     base.css
> manage.py
> setting.py
> ..

File settings.py:

STATIC_URL = '/static/'

File base.html:

{% load static %}
...   
  <link rel="stylesheet" href="{% static 'css/base.css' %}">

Getting error, not found:

GET http://192.168.XX.XX:8000/static/css/base.css

Debug in settings.py is set to true (means static files should be surved). The link looks correct. Now why doesn't this work?

Edit: looks like I forgot to run 'collectstatic'. Here is the output after running it:

enter image description here

Edit2:

EXTERNAL_APPS = [
  ..
  'django.contrib.staticfiles',
]
PROJECT_NAME = 'fooproject'
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_ROOT = os.path.join(BASE_DIR, PROJECT_NAME, 'staticfiles')
STATIC_URL = '/static/'
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I have managed to resolve my problem with the help from ChristophBluoss and DanielRoseman. There were 2 problems:

  1. I haven't ran python manage.py collectstatic.
  2. The config missed crucial settings - STATIC_ROOT and STATICFILES_DIRS.

After adding those variables into settings.py file and successfully running collectstatic - everything worked fine.

P.S. One should read Django documentation more carefully. It wasn't stated clear enough for me that collectstatic is the mandatory thing.

EDIT: As DanielRoseman suggested collectstatic along with STATIC_ROOT is non-mandatory when DEBUG is set to True. I have tried to remove staticfiles and STATIC_ROOT - and it still works in my development environment. So STATICFILES_DIRS was the missing setting.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...