简体   繁体   中英

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!

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! 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. 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.

A con is that, if and when you edit the media, you need to also restart the apache, for the media to refresh.

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).

You can set the MEDIA_URL in your settings, where you point it to the url with the appropriate port.

First, mod_python is not recommended. In fact, it's specifically recommended against . Use mod_wsgi instead.

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. Then, set STATIC_URL in your settings.py to the address served by that server. It makes no difference what port it is on while you're in development.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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