首页 > 解决方案 > 如何在 Django 中提供静态媒体文件?

问题描述

我是 Web Server Apache 的初学者。我想问你们如何在 Django 中提供媒体文件。

我读到这个:https ://docs.djangoproject.com/en/2.0/howto/deployment/wsgi/modwsgi/#serving-files

This example sets up Django at the site root, but serves robots.txt, favicon.ico, and anything in the /static/ and /media/ URL space as a static file. All other URLs will be served using mod_wsgi:

Alias /robots.txt /path/to/mysite.com/static/robots.txt
Alias /favicon.ico /path/to/mysite.com/static/favicon.ico

Alias /media/ /path/to/mysite.com/media/
Alias /static/ /path/to/mysite.com/static/

<Directory /path/to/mysite.com/static>
Require all granted
</Directory>

<Directory /path/to/mysite.com/media>
Require all granted
</Directory>

WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py

<Directory /path/to/mysite.com/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
If you are using a version of Apache older than 2.4, replace Require all granted with Allow from all and also add the line Order deny,allow above it.

我问 Web Server 支持者,他们说我的服务器已经支持 mod_wsgi 模式,他们对我说我可以在.htaccess中配置它。但我真的对此一无所知。请帮我重写网址:

mydomain.com/served/用 path 将URL 重写为文件夹/home/mydomain.com/public_html/media。中的所有文件都/home/mydomain.com/public_html/media/filename.jpg将使用mydomain.com/served/

标签: django

解决方案


您应该能够使用该Alias指令为相关媒体文件夹设置别名/served/。例如:

Alias /served/ /home/example.com/public_html/media/

这意味着对的请求http://example.com/served/filename.jpg将映射到/home/example.com/public_html/media/filename.jpg。您可以对/static/.

(请注意,上面的示例使用example.com而不是mydomain.com由于SO 限制。)


推荐阅读