Django Static files 404

DjangoStaticHttp Status-Code-404

Django Problem Overview


I can't get my static files to come up. I've tried various settings and directory configurations and so on, but they just turn up as 404s. I have debug_toolbar installed so know that STATIC_URL is reaching my request context.

Directory structure showing /static (I have also placed the directory inside of the meals app folder, and users, just to try it out.

/mealmate
    /mealmate
    /meals
    /static
        /css
             /bootstrap.min.css
    /templates
    /users

Settings.py (a few important settings though I've experimented with a variety of other ones):

MEDIA_ROOT = os.path.join(PROJECT_PATH, 'media/')

STATIC_URL = '/static/'

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
)

WSGI_APPLICATION = 'mealmate.wsgi.application'

In base.html rendered

    <link rel="stylesheet" href="/static/css/bootstrap.min.css">

Any ideas? Thanks

Django Solutions


Solution 1 - Django

This is the working solution for static/media/template access in django for windows,

settings.py

import os.path

STATIC_ROOT = ''

STATIC_URL = '/static/'

STATICFILES_DIRS = ( os.path.join('static'), )

Solution 2 - Django

For me this turned out to be caused by setting debug to false in settings.py. A workaround is to pass the --insecure switch to runserver, but the real solution is to use a proper web server to serve the static files. See the answers to this question for more details.

Solution 3 - Django

If you are running this on a web server are you copying the static files to a public accessible folder? For example:

# web accessible folder
STATIC_ROOT = '/home/your_name/www/mealmate/static/'

# URL prefix for static files.
STATIC_URL = '/static/'

# Additional locations of static files
STATICFILES_DIRS = (
    # location of your application, should not be public web accessible	
    '/home/your_name/website/mealmate/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',
)

Then you can use this post https://stackoverflow.com/questions/9824359/django-static-files and copy the static files to the public accessible folder using manage.py

# --link    Create a symbolic link to each file instead of copying.
# --noinput Do NOT prompt the user for input of any kind.
#
python manage.py collectstatic -link --noinput

Hope that helps!

Solution 4 - Django

from comments above - run this

python manage.py findstatic --verbosity 2 css/styles.css

> No matching file found for 'css/styles.css'. > > Looking in the following locations:
> /Users/yourname/Documents/Workspace/test/staticfiles

I simply renamed my static folder to staticfiles and all was well. (I'm on osx + django 1.x)

use insecure mode may not hurt if you're on local dev box - otherwise you may still get 404 errors.

python manage.py runserver --insecure

UPDATE

actually digging into settings.py found the infringing line.

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'staticfiles'),
)

Solution 5 - Django

If you recently changed the debug to false in settings file. Follow this procedure.

Most of the time it is caused by debug setting to be false in settings.py. If you pass the --insecure switch to runserver it should work. So if you do python manage.py runserver 0.0.0.0:8000 change it to python manage.py runserver 0.0.0.0:8000 --insecure instead.

It should work.

Solution 6 - Django

I simply added the equivalent of

STATICFILES_DIRS = (
    '/absolute_path_to_project/mealmate/static',
)

to get this working. Of course, replace absolute_path_to_project with your actual path and, if necessary, add the drive letter.

Solution 7 - Django

I was also stuck in the 404 problem until I realized that Apache had blocked the requests to the static dir. I used python manage.py collectstatic to copy all the static files to the static dir under my project root, i.e. /var/my/site/static. With

Alias /static /var/my/site/static
<Directory /var/my/site/static>
        Require all granted
</Directory>

in /etc/httpd/conf/httpd.conf, it works properly now.

If none of the answers above works, you may consider checking your server config.

Solution 8 - Django

In my case, all I had to do was re-run the server:

python manage.py runserver

Solution 9 - Django

I'm assuming you're using Django1.3+ here.

First off, you need to define a few more settings:

STATICFILES_FINDERS = [
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]

STATICFILES_DIRS = [
    path.join(TOP_DIR, 'static'),
]

STATIC_ROOT = path.join(TOP_DIR, 'staticfiles')
STATIC_URL = '/static/'

This should help you find that directory.

Also, you should always access your static files using STATIC URL:

<link rel="stylesheet" href="{{ STATIC_URL }}css/bootstrap.min.css">

Solution 10 - Django

Make sure mealmate is in your INSTALLED_APPS

Solution 11 - Django

in Django you have one more advantage for your templates and it's you can link statics like this and maybe this is your problem!

<script src="{% static 'myapp/bootstrap.min.js' %}"></script>
<link href="{% static 'myapp/bootstrap.css' %}" rel="stylesheet" type="text/css"/>

its better to use

> {% static 'filename' %}

and its also easier to know more about, i refer to https://docs.djangoproject.com/en/1.11/intro/tutorial06/

Solution 12 - Django

add below in setting.py

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static'),
]

Solution 13 - Django

in my case my static files are in resources/static directory and I set some settings like blow in settings.py file :

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

and using it in my template like this:

% load static %}
    <link rel="stylesheet" type="text/css" href="{% static '/plugins/bootstrap/css/bootstrap.css' %}">

this way worked for me.

Solution 14 - Django

If you are suffering from this error then first check that is the debug true or false and are you using 'STATIC_ROOT' and 'STATICFILES_DIRS' together

For my case I have used the followings in my settings.py:

import os (You have to import os at first)



#(If the app is in production mode then False. By default True)

   DEBUG = False 

#(If debug is false then add allowed hosts)

   ALLOWED_HOSTS = ['*'] 

#(Built in)

   STATIC_URL = '/static/' 

   STATIC_ROOT = 'static' (You have to add it to the code)

   STATICFILES_DIRS = [ os.path.join(BASE_DIR, '/static') ] (You have to add it to the code)

Solution 15 - Django

Insert the following lines into settings.py in the main folder of your project:

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

Then, in the html file insert:

{% load static %}

Finally, within settings.py, add django.contrib.staticfiles into INSTALLED_APPS = [...]

Solution 16 - Django

change DEBUG variable to True in your settings.

the runserver method only looks for statics if debug is enabled, if not, it will look in the STATIC_ROOT url

Solution 17 - Django

I think you missed to put the app INSTALLED_APPS in your settings.py file, you should add "mealmate"

INSTALLED_APPS = (
    'mealmate',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
)

After that the static and command files will be visible to django. I was having trouble with commands and this is the way I fixed it.

Solution 18 - Django

on Cpanel this is good solution here

on terminal on cpanel install dj-static static3

pip install dj-static static3

then in wsgi.py replace this code py application

from dj_static import Cling

application = Cling(get_wsgi_application())

Solution 19 - Django

I faced same issue. Here how I solve of accessing static file in template.

My Django project(Recognition/) file directory look like this:

/media
   /1
     i1.jpg
     i2.jpg
     ....

/static
    /css
       /op.css
    /img
       /emoji.png

/Recognition
    ...
    /settings.py
    /urls.py
    ...

/uploader
    ...
    /urls.py
    /templates
         /uploader
             /create.html
    ...

My settings.py look like this:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'Recognition',
    'uploader',
]

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

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

My (Recognition/urls.py) look like this:

urlpatterns = [
    path('admin/', admin.site.urls),
    ...
]
if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

Then I am accessing all static files and media files in template(create.html) like this: Here is sample code

<!DOCTYPE html>
<html>
<head>
   {% block extrahead %}
      <link rel="stylesheet" type="text/css" href="static/css/op.css">
   {% endblock %}
</head>
       
<style type="text/css">
.notfound .notfound-404 {
  ...
  background-image: url('static/img/emoji.png');
  ...
}
<style>

<body>
  <img src="{{ MEDIA_ROOT }}/media/i1.jpg" class="image_responsive" height="100px" width="100px">
</body>

OR

You can use this django command to see where django is seeing your static files. SOURCE

python manage.py collectstatic --help

Solution 20 - Django

for me, i do nothing but run manage.py runserver myip:port. Then i shut it down and restart uwsgi, the static files load successfully. I don't know why but it works for me...

Solution 21 - Django

I was able to solve this same problem by changing the js filename from camel case to snake case.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKindOfGuyView Question on Stackoverflow
Solution 1 - DjangoAmar KamtheView Answer on Stackoverflow
Solution 2 - DjangoArthur TaccaView Answer on Stackoverflow
Solution 3 - DjangoeodgoochView Answer on Stackoverflow
Solution 4 - DjangojohndpopeView Answer on Stackoverflow
Solution 5 - DjangoKunal KumarView Answer on Stackoverflow
Solution 6 - DjangoSethView Answer on Stackoverflow
Solution 7 - DjangoDarwinView Answer on Stackoverflow
Solution 8 - DjangoDavor JosipovicView Answer on Stackoverflow
Solution 9 - DjangodrewmanView Answer on Stackoverflow
Solution 10 - DjangochrisortmanView Answer on Stackoverflow
Solution 11 - DjangoEbrahim KarimiView Answer on Stackoverflow
Solution 12 - Djangoyasin lachiniView Answer on Stackoverflow
Solution 13 - Djangoahmad_mhmView Answer on Stackoverflow
Solution 14 - DjangoAbindentView Answer on Stackoverflow
Solution 15 - DjangoLinh Thien NguyenView Answer on Stackoverflow
Solution 16 - DjangoWesley BenicioView Answer on Stackoverflow
Solution 17 - DjangoOmar AlvaradoView Answer on Stackoverflow
Solution 18 - DjangoYoussri Abo ElseodView Answer on Stackoverflow
Solution 19 - DjangoHarshit MahajanView Answer on Stackoverflow
Solution 20 - Djangoli vectorView Answer on Stackoverflow
Solution 21 - DjangoRyan wView Answer on Stackoverflow