Reputation: 359
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
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.
'django.contrib.staticfiles'
{% static '<app_name>/blah.js' %}
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
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
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
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