Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: static file not found

Tags:

python

css

django

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/'
like image 462
0leg Avatar asked Dec 02 '16 09:12

0leg


People also ask

How can you set up static files in Django?

Configuring static filesMake sure that django.contrib.staticfiles is included in your INSTALLED_APPS . In your templates, use the static template tag to build the URL for the given relative path using the configured STATICFILES_STORAGE . Store your static files in a folder called static in your app.

How do I serve static files in Django?

Serving the site and your static files from the same serverPush your code up to the deployment server. On the server, run collectstatic to copy all the static files into STATIC_ROOT . Configure your web server to serve the files in STATIC_ROOT under the URL STATIC_URL .

Where are Django static files stored?

Files are searched by using the enabled finders . The default is to look in all locations defined in STATICFILES_DIRS and in the 'static' directory of apps specified by the INSTALLED_APPS setting.

What is static file in Django?

Aside from the HTML generated by the server, web applications generally need to serve additional files — such as images, JavaScript, or CSS — necessary to render the complete web page. In Django, we refer to these files as “static files”.


1 Answers

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.

like image 183
0leg Avatar answered Oct 05 '22 11:10

0leg