1

我有一个 django 项目,我终于设法使用 apache 服务。我想变得非常简单,所以尽管我的测试服务器提供 /media 和静态服务,但我没有将它们包含在我的 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>

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

我的网址.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 错误,第二次打开网页正常。

错误日志

[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 上仍然出现相同的错误。

4

1 回答 1

1

这是因为您专门添加了一些 django 媒体服务 URL 模式。你的担心是对的!

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

您正在MEDIA_ROOT通过MEDIA_URLpython 服务;不推荐,除非在开发期间。

您应该将该添加包含在一个if settings.DEBUG = True语句中。

if settings.DEBUG:
   urlpatterns += static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)
于 2014-05-19T17:10:26.150 回答