[Answer]-Cannot serve static files, Django 1.6

1👍

I’m not sure what is wrong with your setup, but this are the settings that work for me:

settings.py

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

I haven’t defined STATIC_ROOT or STATIC_FINDERS so it’s using the defaults. I have nothing special in urls.py for static files, just one line for media:

urls.py

urlpatterns = patterns('',
    #...
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

The static tag is working fine from the templates so I believe your line in urls.py is not absolutely needed.

0👍

You shouldn’t be serving static content through a web framework, that’s a job for the webserver (apache or nginx for example)

If you have STATIC_URL = '/static/' in your settings.py file you can put "{{ STATIC_URL }}css/base.css" in your templates for example, and have the web server serve /static/ statically, bypassing django completely.

👤GP89

0👍

In your urls.py try to tell django standard views to serve your static files by changing
the urlpatterns

 urlpatterns = patterns('',
   #...
 ) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

 With the following:

 urlpatterns = patterns('',
   #...
 )
 if settings.DEBUG:
 urlpatterns += patterns('',
   (r'^static/(?P<path>.*)$', 'django.views.static.serve',
   {'document_root':settings.STATIC_ROOT}),)


 In the settings.py add:

 import os.path
 STATIC_ROOT ='only_the_path_to_the_project'
 STATICFILES_DIRS = (
  os.path.join(os.path.dirname(__file__),'staticcc').replace('\\','/'),
  )

0👍

In development
In your app folder create folder name ‘static’ and save your picture in that folder.

To use picture use:

<html>
    <head>
       {% load staticfiles %} <!-- Prepare django to load static files -->
    </head>
    <body>
        <img src={% static "image.jpg" %}>
    </body>
</html>

In production:
Everything same like in development, just add couple more parameters for Django:
1. add in settings.py
STATIC_ROOT = os.path.join(BASE_DIR, "static/")(this will prepare folder where static files from all apps will be stored)

2. be sure your app is in INSTALLED_APPS = ['myapp',]

3. in terminall run command python manage.py collectstatic (this will make copy of static files from all apps included in INSTALLED_APPS to global static folder – STATIC_ROOT folder )

Thats all what Django need, after this you need to make some web server side setup to make premissions for use static folder. E.g. in apache2 in configuration file httpd.conf (for windows) or sites-enabled/000-default.conf. (under site virtual host part for linux) add:

 Alias \static "path_to_your_project\static"

<Directory "path_to_your_project\static">
    Require all granted
</Directory>

And that’s all

Leave a comment