django
  • django-templates
  • 2011-12-03 22 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服务器提供静态内容,没有动态的东西)。

    希望这有助于有点...

    相关问题