2013-04-28 177 views
0

我做了一个简单的页面来在django中提供一个静态CSS,但我总是得到一个PAGE NOT FOUND错误。Django和Apache不提供静态文件

Views.py:

def login_user(request): 


     return render_to_response('new_file.html') 

urls.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/' 
+0

您的网址在'static'指指点点,而你指的是'site_media'在你的模板。可能是这个问题? – alecxe 2013-04-28 21:12:17

回答

0

尝试这样的:

<link href="{{STATIC_URL}}css/newstyle.css" rel="stylesheet" type="text/css" />