Django和apache不提供静态文件

时间:2013-04-28 20:03:51

标签: python django apache

我创建了一个简单的页面来为django中的静态CSS提供服务,但我总是得到一个PAGE NOT FOUND错误。

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/'

1 个答案:

答案 0 :(得分:0)

试试这样:

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