Reputation: 3500
I know there are a ton of similiar threads on SO with actually the same question - but I can't solve my problem
Django Version
>>> import django
>>> print django.get_version()
1.6.2
I want to access static files (css, images, ..) from a template.
<link rel="stylesheet" type="text/css" href="styles.css" />
My folder structure
Project
|-- Project
|-- settings.py
|-- ...
|-- app
|-- views.py
|-- ...
|-- templates
|-- a_template.html
|-- static
|-- styles.css
settings.py (important parts)
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'server',
)
STATIC_URL = '/static/'
I tried a lot different solutions for STATIC_URL
(even absolute paths) but none of them worked for me.
Upvotes: 2
Views: 3732
Reputation: 43
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
Upvotes: 0
Reputation: 1807
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' %}">
Upvotes: 1
Reputation: 6388
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.
During deployment you should issue a
$ python manage.py collectstatic
command, which copies all static files to your STATIC_ROOT
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.
Upvotes: 1
Reputation: 2790
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...
Upvotes: 1
Reputation: 699
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/
Upvotes: 0