Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango: Serving Media Behind Custom URL
    text
    copied!<p>So I of course know that serving static files through Django will send you straight to hell but I am confused on how to use a custom url to mask the true location of the file using Django. <a href="https://stackoverflow.com/questions/2681338/django-serving-a-download-in-a-generic-view">Django: Serving a Download in a Generic View</a> but the answer I accepted seems to be the "wrong" way of doing things.</p> <p>urls.py:</p> <pre><code>url(r'^song/(?P&lt;song_id&gt;\d+)/download/$', song_download, name='song_download'), </code></pre> <p>views.py:</p> <pre><code>def song_download(request, song_id): song = Song.objects.get(id=song_id) fsock = open(os.path.join(song.path, song.filename)) response = HttpResponse(fsock, mimetype='audio/mpeg') response['Content-Disposition'] = "attachment; filename=%s - %s.mp3" % (song.artist, song.title) return response </code></pre> <p>This solution works <em>perfectly</em> but not perfectly enough it turns out. How can I avoid having a direct link to the mp3 while still serving through nginx/apache?</p> <p><strong>EDIT 1 - ADDITIONAL INFO</strong></p> <p>Currently I can get my files by using an address such as: <code>http://www.example.com/music/song/1692/download/</code> But the above mentioned method is the devil's work.</p> <p>How can I accomplished what I get above while still making nginx/apache serve the media? Is this something that should be done at the webserver level? Some crazy mod_rewrite?</p> <p><code>http://static.example.com/music/Aphex%20Twin%20-%20Richard%20D.%20James%20(V0)/10%20Logon-Rock%20Witch.mp3</code></p> <p><strong>EDIT 2 - ADDITIONAL ADDITIONAL INFO</strong></p> <p>I use nginx for my frontend and reverse proxy back apache/development server so I think if it does require some sort of mod_rewrite work I will have to find something that would work with nginx.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload