Can't Get Django To Serve Static Files
Solution 1:
first, I would use the static
template tag by adding to the top of the template:
{% load static %}
and then, in the href:
href="{% static "styles.css" %}"
then, I would change my urls.py
and add the possibility for your development server to serve static files:
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
/* you url patterns here */
if settings.DEBUG:
urlpatterns += staticfiles_urlpatterns()
that should do the trick...
Solution 2:
Locating the static files
Since your static
dir doesn't live in an app, you should add os.path.join(BASE_DIR, 'static')
to STATICFILES_DIRS. Much like you probably added os.path.join(BASE_DIR, 'templates')
to your TEMPLATE_DIRS. (at least I think so, as you're not complaining about missing templates.
Collecting the static files (not needed during dev)
During deployment you should issue a
$ python manage.py collectstatic
command, which copies all static files to your STATIC_ROOT
Using the static files in your template
To point your browser to the right resource you should either put {{ STATIC_URL }}
before the filenames you use or use the {% static %} templatetags as in the how to.
Solution 3:
Configure BASE_DIR:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
Set Debug is True:
DEBUG=True
Set Static URl:
STATIC_URL = '/static/'
Set ROOT_PATH In ROOT variable
ROOT = os.path.join(BASE_DIR, 'static')
Set STATICFILES Variable:
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),]
Mention the Static css file in HTML file:
{% load static %}
<link rel="stylesheet"type="text/css" href="{% static 'style.css' %}">
Solution 4:
You want to change the href attribute in the template:
href="{{STATIC_URL}}styles.css"
Additionally, you may want to collect static files via python manage.py collectstatic
.
More info here: https://docs.djangoproject.com/en/dev/howto/static-files/
Solution 5:
To get django to serve bootstrap files, I had to let collectstatic put the files into my STATIC_ROOT--I could NOT paste the files into the folder myself. I put the bootstrap files in a directory called my_app/boot.
updated my settings:
STATIC_URL = '/static/'STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'boot'),
]
and ran
python manage.py collectstatic
Post a Comment for "Can't Get Django To Serve Static Files"