繁体   English   中英

Apache Django和静态和媒体文件服务

[英]Apache django and static and media file serving

我有一个django项目,我终于设法使用apache服务了。 我想非常简单,因此尽管我的测试服务器提供/ media和static服务,但没有将它们包含在site.conf文件中。 我想先检查部署是否可行,然后让apache提供静态文件。 但是,老实说,Apache无需提供任何服务即可从媒体文件夹提供文件。 我的apache conf是

<VirtualHost *:80>
    ServerAdmin webmast@rhombus.com
    ServerName myrhombus.com
    ServerAlias www.myrhombus.com
    WSGIScriptAlias / /srv/www/rhombus2/rhombus/wsgi.py
    <Directory /srv/www/rhombus2/rhombus>
    <Files wsgi.py>
            Require all granted
    </Files>
    </Directory>

    Alias /static/ /srv/www/rhombus2/static/
    Alias /media/ /srv/www/rhombus2/media/

    <Directory /srv/www/rhombus2/static>
        Require all granted
    </Directory>

    <Directory /srv/www/rhombus2/media>
        Require all granted
    </Directory>


</VirtualHost>

如您所见,没有媒体或静态别名。

我的urls.py

urlpatterns = patterns('',
    # Examples:
    #differnet  urls here etc...
    # 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)),
)

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)

这怎么可能?

编辑:我做了您描述的更改,但是现在我有了一种奇怪的态度:)我没有得到任何媒体或静态投放(403错误),并且首先单击任何链接或地址栏中的错误400,第二次打开网页正常。

error.log中

[Tue May 20 10:12:56.049081 2014] [authz_core:error] [pid 1360:tid 140612925908736] [client 127.0.0.1:48360] AH01630: client denied by server configuration: /serv, referer: http://www.myrhombus.com/accounts/login/

当我访问该网站时,我收到一个错误的请求(400)。 如果我再次单击,它将正常打开该站点,但是在error.log上仍然出现相同的错误。

这是因为您专门添加了一些提供URL模式的django媒体。 您是值得关注的!

+static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)

您正在通过python在MEDIA_URL服务MEDIA_ROOT ; 不建议在开发期间使用。

您应该将该附加内容包装在if settings.DEBUG = True语句中。

if settings.DEBUG:
   urlpatterns += static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM