trying to load css/images in django
I looked http://docs.djangoproject.com/en/dev/howto/static-files/ already, but am still confused on how to get css/image files loaded.
On my server, the images folder and css file are located at /srv/twingle/search
my urls.py
1 from django.conf.urls.defaults import *
2
3 # Uncomment the next two lines to enable the admin:
4 # from django.contrib import admin
5 # admin.autodiscover()
6
7 urlpatterns = patterns('twingle.search.views',
8 url(r'^$', 'index'),
9 url(r'^search/(?P<param>\w+)$', 'index'),
10
11 (r'^site_media/(?P<path>.*)$', 'django.views.static.serve',
12 {'document_root': '/srv/twingle/search'}),
13
14 )
I try to access my css as follows,
<link rel="stylesheet" type="text/css" href="/site_media/style.css" />
That's exactly how the tutorial s开发者_Go百科ays to do it, but it doesn't work. Any suggestions?
You've closed the urlpatterns on line 10, so your site_media declarations are just sitting there, not attached to anything. Get rid of the extra close bracket on 10.
Edited to add You've also used the prefix argument, which is being applied to the static view as well. Do this:
urlpatterns = patterns('twingle.search.views',
url(r'^$', 'index'),
url(r'^search/(?P<param>\w+)$', 'index'),
)
urlpatterns += patterns('',
(r'^site_media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': '/srv/twingle/search'})
)
Only use django.views.static.serve
for development or testing. By the way, your rule on static files isn't written as an argument for the patterns
function. It sould be something like this:
from django.conf.urls.defaults import *
# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()
urlpatterns = patterns('twingle.search.views',
url(r'^$', 'index'),
url(r'^search/(?P<param>\w+)$', 'index'),
url(r'^site_media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': '/srv/twingle/search'}),
)
You should write the rules directly to the webserver configuration to match the media url. Or use a different vhost for static files.
Not in the django configuration
精彩评论