将两个MEDIA_URL映射到相同的MEDIA_ROOT [英] Mapping two MEDIA_URLs to the same MEDIA_ROOT

查看:58
本文介绍了将两个MEDIA_URL映射到相同的MEDIA_ROOT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将网站从WordPress迁移到Django/Wagtail.我的 media 目录中有所有旧的WP内容,一切都得到适当的服务.

I’m migrating a website from WordPress to Django/Wagtail. I have all the old WP content in my media directory & it’s all being served appropriately.

为了将旧的URL硬编码在内容中,将其他URL(特别是/wp-content/)映射到 MEDIA_ROOT 将很方便.

It would be convenient to map other URLs (specifically /wp-content/) to MEDIA_ROOT, for the sake of old media URLs that were hardcoded in the content.

例如,现在可以从//example.com/提供迁移资产,该资产现在位于//example.com/media/uploads/2017/12/IMG_2120.jpg wp-content/uploads/2017/12/IMG_2120.jpg

So for example a migrated asset now available at //example.com/media/uploads/2017/12/IMG_2120.jpg can also be served from //example.com/wp-content/uploads/2017/12/IMG_2120.jpg

我敢肯定有一些明显的方法可以做到这一点(在 urls.py 中?),但完全是空白.

I’m sure there’s some obvious way to do this (in urls.py?) but totally drawing a blank.

推荐答案

我确定您已经知道应该使用前端服务器(例如Nginx)来提供静态/媒体文件,因为在很多地方都提到了静态/媒体文件文档.

I'm sure you already know that static/media files should be served using a frontend server (like Nginx), because it's been mentioned at so many places in the docs.

因此,如果Django不提供文件,为什么它需要 MEDIA_ROOT MEDIA_URL 设置?

So, if Django doesn't serve the files, why does it need the MEDIA_ROOT and MEDIA_URL settings?

MEDIA_ROOT 是Django存储您上传的图像/文件的地方.

MEDIA_ROOT is the place where Django stores the images/files you upload.

MEDIA_URL 生成文件URL.例如,如果 MEDIA_URL ='/media/',那么如果您执行 {{image.url}} ,则Django将生成类似以下网址-/media/image.jpg .

MEDIA_URL is used by Django to generate file urls. For example, if MEDIA_URL = '/media/', then if you do {{ image.url }}, Django will generate a url like this - /media/image.jpg.

但是Django不提供文件.您的前端服务器可以.因此,您要做的就是像这样配置前端服务器:

But Django doesn't serve the files. Your frontend server does. So, what you do is, you configure your frontend server like this:

if request path starts with /media/:
    map it to <media directory>

基本上,您是在告诉前端服务器为以/media/开头的每个请求提供< media目录> 中的内容.这样,以/media/开头的请求实际上不会到达您的Django应用,因为您的服务器正在处理它们.

Basically, you're telling your frontend server to serve content from the <media directory> for every request that starts with /media/. This way, a request starting with /media/ never actually reaches your Django app, because your server is taking care of them.

我的意思是,您可以配置前端服务器以将/wp-content/uploads/映射到您的< media directory> 提供文件.

What I mean by all this is that you can configure your frontend server to map /wp-content/uploads/ to your <media directory> and it will serve the files.

这篇关于将两个MEDIA_URL映射到相同的MEDIA_ROOT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆