Cannot load static content in Django
I am setting up a beginner django app and I cannot make it display static content. (css and jpg) files. The documentation is confusing and I need some help at a very basic level.
My static files are located in: '/home/me/django_projects/myproject/site_media'. Under here there are directories: 'images' and 'css'
I am getting 404 errors on everything static. Even if I try to pull the file directly from: http://localhost:8000/site_media/images/test_image.jpg
Here is the html code that I that is supposed to trigger static content:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="keywords" content="" />
<meta name="description" content="" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Django Test </title>
<link href="{{ STATIC_URL }}/css/style.css" rel="stylesheet" type="text/css" media="screen" />
</head>
<body>
This is my body... blah blah blah
<p>Here is an image: <img src="{{ MEDIA_URL }}images/media.jpg" alt="Foobar" />
</body>
Below are my settings files. I've tried about every combination of media and static configuration I can think of. Does anyone see what I am doing wrong here?
File: settings.py
# Example: "/home/media/media.lawrence.com/media/"
MEDIA_ROOT = '/home/me/django_projects/myproject/site_media/'
MEDIA_URL = '/media/'
ADMIN_MEDIA_P开发者_开发问答REFIX = '/admin_media/'
STATIC_ROOT = ''
STATIC_URL = '/static/'
STATICFILES_DIRS = (
"/home/me/django_projects/myproject/static",
)
# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
#...
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'django.contrib.admindocs',
'jim',
)
File: urls.py
urlpatterns = patterns('',
(r'^customers/$', 'jim.views.index'),
(r'^customers/(?P<customer_id>\d+)/$', 'jim.views.detail'),
(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': '/media'}),
(r'^site_media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': '/media'}),
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', include(admin.site.urls)),
)
Does anyone see whats wrong here?
Looks like you might have two forward slashes:
STATIC_URL = '/static/'
"{{ STATIC_URL }}/css/style.css"
would give you
"/static//css/style.css"
view the page source in the browser and see how the url is being written
If I look at your config I would expect the file at:
http://localhost:8000/media/images/test_image.jpg
But please read the django docs on static versus media. Media is for useruploaded content, static is for site-content like css/images.
精彩评论