简体   繁体   English

在Django开发服务器上通过Apache服务媒体文件?

[英]Serve Media Files Via Apache on a Django Development Server?

For some reason i can't figure out, other than the 'stupid' errors that keep creeping up when I try to access media files (files uploaded by the user) in my Django app, why I just can't server media files! 由于某些原因,除了我尝试访问Django应用程序中的media文件(用户上传的文件)时不断出现的“愚蠢”错误外,我无法弄清楚为什么我不能管理media文件!

In this particular case, all I want for example is to be able to serve up images to the front that have been uploaded. 在这种情况下,例如,我想要做的就是能够将上传的图像显示在最前面。 My up correctly serves static files via /static/ , but when I try to serve my /site_media/ files hell breaks loose! 我的up通过/static/正确地提供了静态文件,但是当我尝试为/site_media/提供服务时,地狱就/site_media/松散了! What could I be doing wrong? 我可能做错了什么?

So, after realizing that Django wasn't essentially crafted to actually handle media files, I decided to resort to using Apache via the recommended mod_python option like it is recommended to do in production. 因此,在意识到Django实际上并没有真正处理媒体文件的能力之后,我决定通过建议的mod_python选项使用Apache,就像在生产中建议的那样。 But I've never done this before, and am wondering whether this is worth the trouble on the development server. 但是我以前从未做过,并且想知道在开发服务器上这样做是否值得。

Well, I know eventually I have to go down this path when I go production, and so will still have to learn how to do this, but what are the pros and cons for this route on the development server? 好吧,我知道最终我在生产时必须走这条路,所以仍然必须学习如何做到这一点,但是在开发服务器上使用此路由的利弊是什么?

It is surely a pro since the django serves the requests faster without having to deal with the media. 由于django无需处理媒体即可更快地处理请求,因此肯定是一个专业人士。

A con is that, if and when you edit the media, you need to also restart the apache, for the media to refresh. 缺点是,如果并且在编辑媒体时,还需要重新启动apache,以刷新媒体。

update based on your comment: 根据您的评论进行更新:

You can of-course easily do it. 当然,您可以轻松地做到这一点。 One simple way I practice this is, by using the nginx and symlinking the media folder into nginx sites-enabled and run nginx on port 80 (or any other). 我练习此问题的一种简单方法是,使用nginx并将媒体文件夹符号链接到已启用nginx站点并在端口80(或任何其他端口)上运行nginx。

You can set the MEDIA_URL in your settings, where you point it to the url with the appropriate port. 您可以在设置中设置MEDIA_URL,将其指向具有适当端口的URL。

First, mod_python is not recommended. 首先, 建议使用mod_python。 In fact, it's specifically recommended against . 实际上,特别推荐反对 Use mod_wsgi instead. 请改用mod_wsgi。

Secondly, we have no possible way of telling what you're doing wrong when serving static media via the dev server, because you have provided no code or details of your setup. 其次,由于您未提供任何代码或安装细节,因此我们无法通过开发服务器告诉静态媒体时您在做什么错。

Finally, there is no reason why you can't use Apache - or even better, a lightweight server such as nginx - and point it at your static directory only. 最后,没有理由没有理由不能使用Apache-甚至更好的是使用轻量级服务器(例如nginx)并将其仅指向静态目录。 Then, set STATIC_URL in your settings.py to the address served by that server. 然后,将STATIC_URL中的STATIC_URL设置为该服务器提供的地址。 It makes no difference what port it is on while you're in development. 在开发过程中,端口是什么都没有关系。

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

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