Reputation: 43
I have had this problem for a while now, I have been trying to move my CSS file all over the place and changing the settings, and even messing with the media url stuff, which I don't believe I should be now that I have read other questions.
My CSS file is in the /home/justin/test/static/ directory. My templates are in the /test/templates directory.
My settings are:
STATIC_ROOT = '/home/justin/test/static/'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
'/home/justin/test/static/',
)
My urls are:
urlpatterns = patterns('',
url(r'^$', 'views.home'),
# Static Files
url(r'^static/(?P<path>.*)$','django.views.static.serve', {'document_root':settings.MEDIA_ROOT}),
)
I have all three in my main template.
<link rel="stylesheet" href="/static/style.css" />
<link rel="stylesheet" href="{{ STATIC_URL }}style.css" />
<link rel="stylesheet" href="{{ STATIC_URL }}/style.css" />
They come out:
<link href="/static/style.css" rel="stylesheet"></link>
<link href="style.css" rel="stylesheet"></link>
<link href="/style.css" rel="stylesheet"></link>
The first comes out to the correct file, but it doesn't work. Any help is really appreciated. Thank you.
My views.py:
from django.shortcuts import render_to_response
def home(request):
return render_to_response('index.html',{})
My error from the terminal is:
[16/Aug/2013 21:00:21] "GET /static/style.css HTTP/1.1" 500 1729
Upvotes: 0
Views: 977
Reputation: 1222
In development mode (which means DEBUG=True
in settings.py
), to serve static files
Simply put the following lines in urls.py
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
# ... the rest of your URLconf here ...
urlpatterns += staticfiles_urlpatterns()
In production mode, you should use nginx/apache in front of django to serve static files.
Refs: https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/
There's a line in your question:
url(r'^static/(?P<path>.*)$','django.views.static.serve', {'document_root':settings.MEDIA_ROOT}),
Normally, settings.MEDIA_ROOT
is for user uploaded files, which is a different folder from settings.STATIC_ROOT
.
Upvotes: 0
Reputation: 1066
In your urls, you should serve from the STATIC_ROOT
not the MEDIA_ROOT
setting
url(r'^static/(?P<path>.*)$','django.views.static.serve', {'document_root':settings.STATIC_ROOT})
Also, you need to pass RequestContext on your views, like stated in other answers.
Upvotes: 0
Reputation: 174624
Here are some tips to help you solve this problem.
First, anything to do with MEDIA_
is referring to files that are uploaded to the system by users; and STATIC_
refers to files that are part of your application.
STATIC_ROOT
should point to the file system path where the collectstatic
command will dump all the static files from all your applications. When you are deploying the application into a production environment, you run collectstatic
(which will overwrite everything in the directory pointed to by STATIC_ROOT
) and then copy the contents of this directory to a location that is mapped to the url that STATIC_URL
points to.
In summary:
If your application needs any static files (images, javascript, css, etc.) create a directory called static
in your application's directory (the same location where you have models.py
) and add the files there.
STATIC_ROOT
should point to a directory where all the images, css, javascript, etc. will be dumped by the collectstatic
command. This is only used when you want to move your application to a production server. The collectstatic
command will grab all the files in all the static
directories in your applications that are listed in INSTALLED_APPS
(including the ones from the django.contrib
like the admin interface) and dump them in this directory. You would then take this directory and copy or move it to your web server.
STATICFILES_DIRS
is a directory where any static files that are not part of any specific application should be stored. If this location is set, then django will also look here for static files.
STATIC_URL
= the URL path that is configured in your web server to point to the location where all the files in STATIC_ROOT
are located.
The {% static %}
tag will always point to the STATIC_URL
variable. In order to use this tag you must have {% load staticfiles %}
at the top of any template that is using the tag; even if the template is inheriting from one that already as the load line in it.
You don't need anything fancy in your urls.py
unless you are using django to serve your static files (which you should not do). Use the web server to handle static files. If you are running with DEBUG = True
and using runserver
, then django will take care of handling static files for you automatically as a courtesy during development.
For any user uploaded files which are controlled by the various MEDIA_*
settings you need to handle these manually during development; and for this you can use the following snippet in your urs.py
. Again, keep in mind this is only for development - do not use this in production:
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = patterns('',
# ... the rest of your URLconf goes here ...
)
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
Please read the static files section in the manual; from where I have summarized the above points.
Finally, you should use the render
shortcut when using methods in your views. This makes sure that the proper request context is always sent.
Upvotes: 1
Reputation: 8836
You need to pass request
in RequestContext
of your views
def home(request):
return render_to_response('index.html', locals(), context_instance = RequestContext(request))
Only then your session data will be passed to the template and {{ STATIC_URL }}
will work.
Upvotes: 1
Reputation: 1829
you must use this tag ({% load static from staticfiles %}) in your template :
{% load static from staticfiles %}
<link rel="stylesheet" type="text/css" href="{% static 'style.css' %}" />
Upvotes: 0