在我的 django 应用程序中,我已经把这些设置
settings.py
------------
MEDIA_ROOT = '/home/me/dev/python/django/myproject/myapp/media/'
MEDIA_URL = '/site_media/'
urls.py
--------
...
url(r'^myapp/', include('myapp.urls')),
url(r'^site_media/(?P<path>.*)$','django.views.static.serve',{'document_root':settings.MEDIA_ROOT}),
我在文件夹中创建了一个 css 文件'/home/me/dev/python/django/myproject/myapp/media/css'
并将其包含在模板中,如下所示
base.html
--------
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>my app</title>
<LINK REL=StyleSheet HREF="{{MEDIA_URL}}css/myappstyle.css" TYPE="text/css" MEDIA="screen, print"/>
home.html
---------
{% extends "mywebapp/base.html" %}>
{% block title %}{{block.super}}| Home {% endblock %}
{% block content %}
{{block.super}}
hi
{% endblock %}
然后我创建了一个如下视图
def custom_render(request,context,template):
req_context=RequestContext(request,context)
return render_to_response(template,req_context)
def home(request,template_name,page_title):
print 'home'
return render_to_response(template_name,{'me':'myname'})
并设置它的 url conf
myapp.urls
--------
urlpatterns=patterns('',
url(r'^$','myapp.views.home',
{
'template_name':'mywebapp/home.html',
'page_title':'Home'
},
name='home'),
当我给出网址时
http://127.0.0.1:8000/myapp/
网页被渲染,但 css 不是。控制台输出显示 404 错误
[14/Sep/2012 10:02:50] "GET /myapp/ HTTP/1.1" 200 547
[14/Sep/2012 10:02:50] "GET /myapp/css/myappstyle.css HTTP/1.1" 404 2514
但是,当我给出网址时,
http://127.0.0.1:8000/site_media/css/myappstyle.css
我可以毫无问题地看到 css 文件的内容。
为什么会这样?有人可以帮我解决这个问题吗?