我制作了一个简单的页面来在 django 中提供静态 CSS,但我总是收到 PAGE NOT FOUND 错误。
视图.py:
def login_user(request):
return render_to_response('new_file.html')
网址.py:
from django.conf.urls import patterns, include, url
from bookmarks.views import *
import os
from django.conf import settings
# Uncomment the next two lines to enable the admin:
#from django.contrib import admin
#admin.autodiscover()
site_media=os.path.join(os.path.dirname(__file__),'site_media')
urlpatterns = patterns('',
# Examples:
#url(r'^$', 'hello.views.home', name='home'),
#url(r'^hello/', include('hello.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
#url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
#url(r'^admin/', include(admin.site.urls)),
(r'^$', main_page),
(r'^login/$', login_user),
(r'^static/(?P<path>.*)$','django.views.static.serve',{'document_root': settings.MEDIA_ROOT})
)
模板 HTML:
<html>
<head>
<title>THIS IS MY PAGE</title>
</head>
<body>
<h1>NEW PAGE</h1>
<link href="/site_media/newstyle.css" rel="stylesheet" type="text/css" />
<p>{{ page_body }}</p>
</body>
</html>
CSS:
#body {color:red;}
settings.py site_media 部分:
MEDIA_ROOT = '/srv/www/hello/site_media/'
# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash.
# Examples: "http://example.com/media/", "http://media.example.com/"
MEDIA_URL = '/site_media/'
# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/var/www/example.com/static/"
STATIC_ROOT = '/srv/www/hello/site_media/'
# URL prefix for static files.
# Example: "http://example.com/static/", "http://static.example.com/"
STATIC_URL = '/site_media/'