django
  • django-templates
  • 2011-12-03 21 views 0 likes 
    0

    我的css文件不在Production server中工作。我部署使用WSGI。你能解決我的問題嗎?謝謝 css linkCSS文件沒有出現在生產服務器

    <link href="{{ MEDIA_URL}}css/style.css" rel="stylesheet" type="text/css" /> 
    

    settings.py

    CURRENT_PATH = '/home/nibbler/code/project/ 
    MEDIA_ROOT = os.path.join(CURRENT_PATH, 'templates/media') 
    
    MEDIA_URL = '/media/' 
    TEMPLATE_DIRS = (
        os.path.join(CURRENT_PATH, 'templates/temp_name'), 
    ) 
    

    site-available\default

    <VirtualHost *:80> 
        ServerAdmin [email protected] 
        ServerName project.org 
        DocumentRoot "/home/nibbler/code/project/" 
        ServerName localhost 
        ErrorLog "/home/nibbler/code/project/logs/apache-error.log" 
        CustomLog "/home/nibbler/code/project/logs/apache-access.log" common 
    
        Options ExecCGI FollowSymLinks MultiViews 
    
        AddHandler wsgi-script .wsgi 
        WSGIDaemonProcess nibbler 
        WSGIProcessGroup nibbler 
    
        Alias /media /home/nibbler/code/project/templates/media/ 
        WSGIScriptAlias//home/nibbler/code/project/apache/django.wsgi 
    
        DirectoryIndex index.html index.cgi 
    
        AddHandler cgi-script .cgi .pl 
    </VirtualHost> 
    

    urls.py

    if settings.DEBUG: 
        # static files (images, css, javascript, etc.) 
        urlpatterns += patterns('', 
         (r'^media/(?P<path>.*)$', 'django.views.static.serve', { 
         'document_root': settings.MEDIA_ROOT})) 
    

    回答

    1

    您有:

    Alias /media /home/nibbler/code/project/templates/media/ 
    

    錯誤的一個開始。嘗試:

    Alias /media/ /home/nibbler/code/project/templates/media/ 
    

    他們必須都有尾部斜線或都沒有它。你不能有一個擁有它,另一個沒有它。

    BTW,有:

    DocumentRoot "/home/nibbler/code/project/" 
    

    是一個壞主意。不要將DocumentRoot設置爲代碼的位置。如果出於某種原因要刪除WSGISriptAlias,則所有代碼都可以由外部人員下載。

    您還缺少一個帶有Allow指令的目錄塊,用於WSGI腳本文件和靜態文件存在的位置。這意味着你已經在這個虛擬主機之外改變了Apache的配置,以某種方式說Apache可以從你的盒子上的任何目錄提供文件,這是一個壞主意,因爲它剝離了一個安全級別。

    +0

    MEDIA_ROOT相同。 settings.py中沒有結尾的斜槓。 –

    +0

    @Graham Dumpleton Ive變了。但不工作。 – Kulbir

    +0

    您是否嘗試過訪問靜態媒體文件?您是否希望他們能夠計算出哪些HTTP錯誤?您是否查看過網頁的HTML源代碼以查看返回的內容是否正確? DIrectory/Allow如何丟失? –

    0

    只有CSS文件或所有媒體文件有問題嗎?

    MEDIA_ROOT - 應該是系統上的文件的完整路徑,看起來你有它,很好。 MEDIA_URL - 嘗試將完整的URL放入媒體文件。嘗試使用完整的網址手動訪問它們,並查看網絡服務器是否正確提供服務。如果是,則將完整的網址放入MEDIA_URL。

    您在if settings.DEBUG上展示的最後一件東西在生產中並不需要。你真的想在生產服務器上設置DEBUG = False。 Django建議有兩個虛擬主機 - 一個用於Django應用程序本身,另一個用於媒體(在那裏你直接用http服務器提供靜態內容,沒有動態的東西)。

    希望這有助於有點...

    相關問題