【问题标题】:Django and apache not serving static filesDjango 和 apache 不提供静态文件
【发布时间】:2013-04-28 20:03:50
【问题描述】:

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

【问题讨论】:

  • 您的网址指向static,而您在模板中指的是site_media。这可能是个问题吗?

标签: python django apache


【解决方案1】:

试试这样:

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

【讨论】:

    猜你喜欢
    • 2014-11-24
    • 2012-03-19
    • 1970-01-01
    • 1970-01-01
    • 2012-12-01
    • 1970-01-01
    • 2016-11-26
    • 2017-09-02
    • 1970-01-01
    相关资源
    最近更新 更多