簡體   English   中英

如何在Django中提供靜態媒體文件?

[英]How to Serve static Media Files in Django?

我是Web服務器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服務器支持者,他們說我的服務器已經支持模式mod_wsgi,他們對我說我可以在.htaccess中進行配置。 但是我真的對此一無所知。 請幫我重寫網址:

將URL mydomain.com/served/重寫為路徑為/home/mydomain.com/public_html/media文件夾。 /home/mydomain.com/public_html/media/filename.jpg所有文件都將使用mydomain.com/served/訪問

您應該只能夠使用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/資源執行類似的操作。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM