2012-01-06 45 views
0

我在Django中遇到了一個特殊的CSS問題。Django Css問題

請參閱下面的代碼。

from django.conf.urls.defaults import patterns, include, url 
    from Search import settings 
    from django.contrib import admin 
    admin.autodiscover() 

    urlpatterns = patterns('Search.views', 
     # Examples: 
     url(r'^$','select_project'), 
     url(r'^(?P<Project_Name>[\w \s]*)$','home'), 
     url(r'^(?P<Project_Name>[\w\s]*/+\w*)$','Management'), 

     url(r'^admin/doc/', include('django.contrib.admindocs.urls')), 
     url(r'^admin/', include(admin.site.urls)), 
    ) 

    if settings.DEBUG: 
     urlpatterns += patterns('', 
      (r'^(?P<path>.*)$', 'django.views.static.serve',{'document_root': settings.MEDIA_ROOT}), 
      (r'^(?P<Project_Name>[\w\s]*/+[\w]*)$', 'django.views.static.serve',{'document_root': settings.MEDIA_ROOT}), 
    ) 

當我的網址--http://127.0.0.1:8000--http://127.0.0.1:8000/xxxxx/,我的CSS是正確加載。
但是,當我從http://127.0.0.1:8000/xxxxx/導航到http://127.0.0.1:8000/xxxxx/abcd,那麼我的css將不會加載。請幫助

我的設置是這樣的:

MEDIA_ROOT=os.path.join(os.path.dirname(__file__), 'templates') 

    MEDIA_URL = '/home/' 

而且,我的網控制檯說:

[06/Jan/2012 08:56:51] "GET /xxxxx/css/main.css HTTP/1.1" 404 1797 
[06/Jan/2012 08:56:51] "GET /xxxxx/css/skin.css HTTP/1.1" 404 1797 
[06/Jan/2012 08:56:51] "GET /xxxxx/javascript/cufon-yui.js HTTP/1.1" 404 1830 
[06/Jan/2012 08:56:51] "GET /xxxxx/javascript/font.font.js HTTP/1.1" 404 1830 
+1

CSS渲染如何?是不是像'../ css/main.css'? – 2012-01-06 06:02:50

+0

它呈現像/home/css/main.css – varadajan 2012-01-06 06:06:49

回答

4

不要使用相對URL的CSS(例如,/你的頁/ CSS /的main.css)。

改爲使用絕對值(例如/static/css/main.css)。

+0

它爲我工作。涼。但是從加載css文件的圖像有問題。相對路徑不適用於Apache。謝謝你Vorushin – varadajan 2012-01-06 16:44:49