uday.blob
uday.blob

Reputation: 359

django not serving static files

I'm using Django==1.5.5 and My django app is structured as

--project/
----project/
------settings.py
------urls.py
----app1/
------models.py
------views.py
----staticfiles/
------style.css
----static/
------admin/
--------js/
--------css/
------style.css
----templates/
------header.html
------post.html
------footer.html
----manage.py

the /project/settings.py is

import os    
DEBUG = False
TEMPLATE_DEBUG = DEBUG    
PROJECT_ROOT = os.path.join(os.path.dirname(__file__), '..')    
ALLOWED_HOSTS = ['localhost', '127.0.0.1']    
MEDIA_ROOT = ''    
MEDIA_URL = ''    
SITE_ROOT = PROJECT_ROOT    
STATIC_ROOT = os.path.join(SITE_ROOT, 'static')    
STATIC_URL = '/static/'    
STATICFILES_DIRS = (
    os.path.join(SITE_ROOT, 'staticfiles'),
)    
TEMPLATE_DIRS = (
    os.path.join(SITE_ROOT, 'templates'),
)

in header.html i trying use it as:

    <head>
        <title>My Site</title>
        {% load static from staticfiles %}
        <link rel="stylesheet" href="{% static 'style.css' %}">
    </head>

But its not loading mysite.com/static/style.css and giving error 404

I ran python manage.py collectstatic to collect all static file.

Why its loading css file? any configuration missing?

Please suggest.

Upvotes: 8

Views: 17948

Answers (4)

Lane Henslee
Lane Henslee

Reputation: 29

Django will NEVER serve files from the STATIC_ROOT. The whole purpose of STATIC_ROOT is to store everything in a folder that gets used at the production level. I've spent 5 hours studying this and I finally get it. I'm going to go over everything for development mode.

People are going to try and tell you that you need to add all this stuff to your urls.py when all you really need to do is add the following.

  1. Add this to the INSTALLED_APPS list in settings.py.

'django.contrib.staticfiles'

  1. At least one of your apps needs to have a folder named static within it and put your static files in there. I recommend making a folder named static within each app with yet another folder named after the app so you do

{% static '<app_name>/blah.js' %}

  1. If you have more js files, you add that folder to the STATICFILES_DIRS list in settings.py. For example, I have a folder named js_imports where I do npm install and store all those files there.

STATICFILES_DIRS = [
  '/home/user/js_imports/node_modules'
 ]

Now, what the heck is python manage.py collectstatic even used for?

This is used to gather all files found within a folder named static within any of your apps as well as files within the STATICFILES_DIRS and puts them into the STATIC_ROOT directory. Now that all your static files are conveniently placed in one folder, you use something like apache to serve them in a more efficient manner during production.

BOOM!

If you don't believe me, understand me, or I missed anything read through this meticulously and try it out https://docs.djangoproject.com/en/4.0/howto/static-files/

One last note. Django conveniently places commented links to what you need to do for each part of the app throughout the code it generates for you.

Upvotes: 2

Kracekumar
Kracekumar

Reputation: 20419

According to docs right way to load static file is

{% load staticfiles %}
<img src="{% static 'my_app/myexample.jpg' %}" alt="My image"/>

This will work

Upvotes: 0

gachokaeric
gachokaeric

Reputation: 110

Only for development. Set DEBUG = True in settings.py file and add 'django.contrib.staticfiles' in INSTALLED_APPS. Next, add these lines of code to settings.py file of your project:

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

Upvotes: 3

lafferc
lafferc

Reputation: 2769

I had the same problem and none of the answers I found worked. It turns out that there are a few parts that must be set up correctly for static files to work. Note: I'm using django 1.6 and this solution is for development not deployment.

Setting up static file dirs

django-admin.py startproject $site_name
mkdir $site_name/static
mkdir $site_name/static/css
mkdir $site_name/static/javascript
mkdir $site_name/static/images

your folder should look like this

$site_name/
    manage.py
    $site_name/
        __init__.py
        settings.py
        urls.py
        wsgi.py
    static/
        css/
        javascript
        images/

edit the $site_name/$site_name/setting.py file and add

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS =( os.path.join(STATIC_ROOT, 'css/'),
                    os.path.join(STATIC_ROOT, 'javascript/'),
                    os.path.join(STATIC_ROOT, 'images/')
                  )

edit $site_name/$site_name/urls.py and add

from django.conf import settings
from django.conf.urls.static import static

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

using static urls

{% load staticfiles %}
<a href="{% static "banner.png" %}">

Upvotes: 8

Related Questions