views:

67

answers:

2

So I want to serve a couple of mp3s from a folder in /home/username/music. I didn't think this would be such a big deal but I am a bit confused on how to do it using generic views and my own url.

urls.py

url(r'^song/(?P<song_id>\d+)/download/$', song_download, name='song_download'),

The example I am following is found in the generic view section of the Django documentations: http://docs.djangoproject.com/en/dev/topics/generic-views/ (It's all the way at the bottom)

I am not 100% sure on how to tailor this to my needs. Here is my views.py

def song_download(request, song_id):
    song = Song.objects.get(id=song_id)

    response = object_detail(
        request,
        object_id = song_id,
        mimetype = "audio/mpeg",
    )
    response['Content-Disposition'= "attachment; filename=%s - %s.mp3" % (song.artist, song.title)

    return response

I am actually at a loss of how to convey that I want it to spit out my mp3 instead of what it does now which is to output a .mp3 with all of the current pages html contained. Should my template be my mp3? Do I need to setup apache to serve the files or is Django able to retrieve the mp3 from the filesystem(proper permissions of course) and serve that? If it do need to configure Apache how do I tell Django that?

Thanks in advanced. These files are all on the HD so I don't need to "generate" anything on the spot and I'd like to prevent revealing the location of these files if at all possible. A simple /song/1234/download would be fantastic.

+1  A: 

Why do you want to do this with a generic view? It's very easy to do this without generic views:

def song_download(request, song_id):
    song = Song.objects.get(id=song_id)
    fsock = open('/path/to/file.mp3', 'r')
    response = HttpResponse(fsock, mimetype='audio/mpeg')
    response['Content-Disposition'] = "attachment; filename=%s - %s.mp3" % \
                                     (song.artist, song.title)
    return response

I'm not sure if it's possible to make this work somehow with a generic view. But either way, using one is redundant here. With no template to render, the context that is automatically provided by the generic view is useless.

Ben James
Well damn, guess I so fixated on getting it to work with a generic view I didn't think why I even needed one. Exactly what I needed, thanks!
TheLizardKing
+3  A: 

Serving static files with Django is a bad idea, use Apache, nginx etc.

http://docs.djangoproject.com/en/1.1/howto/static-files/

Tomasz Zielinski
I hear ya, I am all for using apache but how can I accomplish the accepted answer using Apache as my web server? I want to use my own urls and not reveal my download url.
TheLizardKing
I'm not a static-file-serving-expert, but I bet you can rewrite static media URL as well
Tomasz Zielinski
My contention with the above link are lines like "We recommend using a separate Web server -- i.e., one that's not also running Django -- for serving media. Here are some good choices:" I hear them loud and clear but let's say I have a full path and/or a url to the file, how do I spit that out using my above mentioned url styling? I'm trying to find open source django projects to see how they do it.
TheLizardKing
You should be rather looking at Apache or ngixs configuration and/or modules - it has nothing to do with Django which works behind http server.
Tomasz Zielinski