Django static files result in 404

Ive checked several other threads for not being able to execute static content using a static file application in Django, but have not yet found a solution.

settings.py

STATIC_ROOT = '/opt/django/webtools/static/' STATIC_URL = '/static/' STATICFILES_DIRS = ( "/home/html/static", ) STATICFILES_FINDERS = ( 'django.contrib.staticfiles.finders.FileSystemFinder', 'django.contrib.staticfiles.finders.AppDirectoriesFinder', # 'django.contrib.staticfiles.finders.DefaultStorageFinder', ) 

Template

corresponding line ....

 <img src="{{ STATIC_URL }}macmonster/img/macmonster-logo-blue.png" > 

Magazines

From the magazines, it seems that the path is correct, but, alas, it still leads to 404 ..

 [10/Feb/2013 16:19:50] "GET /static/macmonster/img/macmonster-logo-blue.png HTTP/1.1" 404 1817 [10/Feb/2013 16:19:51] "GET /static/macmonster/img/macmonster-logo-blue.png HTTP/1.1" 404 1817 
+6
source share
2 answers

To download static files locally , if you don’t have it , install any form of collecting static files, and if you are using Django 1.3+, I believe that this is your settings.py should look if you refer to static files

 # Absolute path to the directory static files should be collected to. # Don't put anything in this directory yourself; store your static files # in apps' "static/" subdirectories and in STATICFILES_DIRS. # Example: "/home/media/media.lawrence.com/static/" STATIC_ROOT = '' # URL prefix for static files. # Example: "http://media.lawrence.com/static/" STATIC_URL = '/static/' # Additional locations of static files STATICFILES_DIRS = ( # Put strings here, like "/home/html/static" or "C:/www/django/static". # Always use forward slashes, even on Windows. # Don't forget to use absolute paths, not relative paths. '/Users/cupcake/Documents/Workspaces/myDjangoProject/someOtherFolderPerhapsIfYouWant/static', ) # List of finder classes that know how to find static files in # various locations. STATICFILES_FINDERS = ( 'django.contrib.staticfiles.finders.FileSystemFinder', 'django.contrib.staticfiles.finders.AppDirectoriesFinder', # 'django.contrib.staticfiles.finders.DefaultStorageFinder', ) 

Please note that I did not specify STATIC_ROOT . This is because I don’t have to build static files “yet”.

Collecting static files is to work around (writing) maintenance problems with several different static files, so they combined the staticfiles application, which was usually used to help with this problem. What this does, and described in the docs, is to take all the static files from all of your applications and put them in one (1) folder to simplify maintenance when placing your application in production.

So, the problem is that you “skipped” this step and why you get 404 when you try to access them. Therefore, you need to use the absolute path to your static files , i.e. on a Mac or Unix system, it should look something like this:

 '/Users/cupcake/Documents/Workspaces/myDjangoProject/someOtherFolderPerhapsIfYouWant/static', 

In addition, you can simplify and “fix” the need to have a hard path similar to the one I used to illustrate, and do it

 PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__)) STATICFILES_DIRS = ( PROJECT_ROOT + '/static/' ) 

This will also fix the portability issue. A good Stackoverflow post about this is found here.

Hope I made it a little clearer, otherwise, please correct me if I am wrong ^ _ ^!

To collect and manage static files in new versions of Django, read this link . Staticfiles application

+11
source

Change

 STATIC_URL = '/static/' 

set

 STATIC_URL = 'http://yourdomain.com/static/' 

this is unbelievable, but after 1 hour of searching, its solution will solve my problem with static files and remove STATIC_ROOT from STATICFILES_DIRS . STATICFILES_DIRS intended only for collecting all static in modules and storing them in STATIC_ROOT .

+3
source

All Articles