Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - Static file not found

I've seen several posts for this issue but didn't found my solution.

I'm trying to serve static files within my Django 1.3 development environment.

Here are my settings

... STATIC_ROOT = '/home/glide/Documents/django/cbox/static/' STATIC_URL = '/static/' STATICFILES_DIRS = (   '/static/', ) ... 

My urls.py

urlpatterns = patterns('', ...   url(r'^static/(?P<path>.*)$', 'django.views.static.serve',     {'document_root', settings.STATIC_ROOT}   ), ... ); 

My /home/glide/Documents/django/cbox/static/ directory is like

css   main.css javascript image 

I get a 404 error when trying to access http://127.0.0.1:8000/static/css/main.css.

Do I have to specify patterns for css, javascript and images individually ?

like image 843
Pierre de LESPINAY Avatar asked May 16 '11 08:05

Pierre de LESPINAY


People also ask

What is white noise in Django?

Project description WhiteNoise works with any WSGI-compatible app but has some special auto-configuration features for Django. WhiteNoise takes care of best-practices for you, for instance: Serving compressed content (gzip and Brotli formats, handling Accept-Encoding and Vary headers correctly)

Where are Django static files stored?

Using the collectstatic command, Django looks for all static files in your apps and collects them wherever you told it to, i.e. the STATIC_ROOT . In our case, we are telling Django that when we run python manage.py collectstatic , gather all static files into a folder called staticfiles in our project root directory.

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”.


2 Answers

I confused STATIC_ROOT and STATICFILES_DIRS

Actually I was not really understanding the utility of STATIC_ROOT. I thought that it was the directory on which I have to put my common files. This directory is used for the production, this is the directory on which static files will be put (collected) by collectstatic.

STATICFILES_DIRS is the one that I need.

Since I'm in a development environment, the solution for me is to not use STATIC_ROOT (or to specify another path) and set my common files directory in STATICFILES_DIRS:

#STATIC_ROOT = (os.path.join(SITE_ROOT, 'static_files/')) import os SITE_ROOT = os.path.dirname(os.path.realpath(__file__)) STATICFILES_DIRS = (   os.path.join(SITE_ROOT, 'static/'), ) 

Also don't forget to from django.conf import settings

like image 162
Pierre de LESPINAY Avatar answered Nov 03 '22 02:11

Pierre de LESPINAY


Serving static files can be achieved in several ways; here are my notes to self:

  • add a static/my_app/ directory to my_app (see the note about namespacing below)
  • define a new top level directory and add that to STATICFILES_DIRS in settings.py (note that The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting)

I prefer the first way, and a setup that's close to the way defined in the documentation, so in order to serve the file admin-custom.css to override a couple of admin styles, I have a setup like so:

. ├── my_app/ │   ├── static/ │   │   └── my_app/ │   │       └── admin-custom.css │   ├── settings.py │   ├── urls.py │   └── wsgi.py ├── static/ ├── templates/ │   └── admin/ │       └── base.html └── manage.py 
# settings.py STATIC_ROOT = os.path.join(BASE_DIR, 'static') STATIC_URL = '/static/' 

This is then used in the template like so:

# /templates/admin/base.html {% extends "admin/base.html" %} {% load static %}  {% block extrahead %}     <link rel="stylesheet" href="{% static "my_app/admin-custom.css" %}"> {% endblock %} 

During development, if you use django.contrib.staticfiles [ed: installed by default], this will be done automatically by runserver when DEBUG is set to True [...]

https://docs.djangoproject.com/en/1.10/howto/static-files/

When deploying, I run collectstatic and serve static files with nginx.


The docs which cleared up all the confusion for me:

STATIC_ROOT

The absolute path to the directory where collectstatic will collect static files for deployment.

...it is not a place to store your static files permanently. You should do that in directories that will be found by staticfiles’s finders, which by default, are 'static/' app sub-directories and any directories you include in STATICFILES_DIRS).

https://docs.djangoproject.com/en/1.10/ref/settings/#static-root


Static file namespacing

Now we might be able to get away with putting our static files directly in my_app/static/ (rather than creating another my_app subdirectory), but it would actually be a bad idea. Django will use the first static file it finds whose name matches, and if you had a static file with the same name in a different application, Django would be unable to distinguish between them. We need to be able to point Django at the right one, and the easiest way to ensure this is by namespacing them. That is, by putting those static files inside another directory named for the application itself.

https://docs.djangoproject.com/en/1.10/howto/static-files/


STATICFILES_DIRS

Your project will probably also have static assets that aren’t tied to a particular app. In addition to using a static/ directory inside your apps, you can define a list of directories (STATICFILES_DIRS) in your settings file where Django will also look for static files.

https://docs.djangoproject.com/en/1.10/howto/static-files/

like image 29
ptim Avatar answered Nov 03 '22 03:11

ptim