2👍
✅
This doesn’t look good:
STATIC_ROOT = (
join (base_dir, 'staticfiles')
)
This variable should be a string, not a tuple, like this:
STATIC_ROOT = join(base_dir, 'staticfiles')
But I think you probably want to do this instead:
STATICFILES_DIRS = (
join(base_dir, 'staticfiles')
)
Make sure that base_dir
is an absolute path, and that staticfiles/css/main.css
really exists relative to it.
And since you’re in Django 1.6, it’s better to use {% static 'css/main.css' %}
instead of {{ STATIC_URL }}/css/main.css
. For that you’ll have to do {% load staticfiles %}
earlier in the file, ideally near the top.
The documentation explains very nicely the difference between these variables. In a nutshell:
- During development, if you have
django.contrib.staticfiles
inINSTALLED_APPS
, then Django will discover static files in thestatic
directories in your apps and serve them from there. - You can use
STATICFILES_DIRS
to specify additional directories where Django should look for static files. STATIC_ROOT
is something use in deployment, whenDEBUG=False
. This should be an absolute path in the filesystem, a path used by your web server like Apache (NOT Django’s built-in development server). When you runpython manage.py collectstatic
, Django will gather all static files in your project and copy them to this common location.STATIC_URL
is the base URL of static files. Typically/static/
. In deployment, this URL will not be served by Django, but directly by the web server, from the location ofSTATIC_ROOT
.
0👍
STATIC_ROOT = os.path.join(PROJECT_DIR,'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(PROJECT_DIR,'staticfiles'), # if you name your static files folder as "staticfiles"
)
Now in templates include staticfiles as
<link rel="stylesheet" type="text/css" href="{% static 'css/main.min.css' %}" />
<script type="text/javascript" src="{% static 'js/jquery.min.js' %}"></script>
- [Answered ]-How to get a property of all records in django sqlite3
- [Answered ]-How to use checkboxes in Django Admin for a ManyToMany field through a model
- [Answered ]-How to make reverse URL to current instance of application?
- [Answered ]-Django not reflecting changes to urls.py
Source:stackexchange.com