How to I create download link of some file using django

22,627 views
Skip to first unread message

Kann

unread,
May 26, 2011, 5:34:20 AM5/26/11
to Django users
Dear all,

I am new to django and still kinda confused about how to handle
download link using django. What I did up to now was having a file
handle and point that handle to django's File object. So, now I have
django file object but I don't know how to incorporate that file
object into my template. Doing somefile.name will only give the file
name on the template, though; but how do I create a downloadable link
for that file from that on my template?

Best,

Kann

Florian Apolloner

unread,
May 26, 2011, 6:15:13 AM5/26/11
to Django users
Hi,

file.url is what you are looking for (assuming MEDIA_URL is configured
correctly)

Cheers,
Florian

Kann

unread,
May 26, 2011, 6:30:11 AM5/26/11
to Django users
Thanks Florian, but I am still confused about what's happening. Below
is the concept of my codes and perhaps you can help:

from django.core.files import File

some_file = open('bla/bla/bla/', "rw")
django_file = File(some_file)

t = loader.get_gemplate('somewhere/temp.html')
c = Context({'file':django_file})
return HttpResponse(t.render(c))


### in the template ####
<a href="file:///{{ django_file.url }}>download</a>


Should this be a proper way to do?

Kann

Boštjan Mejak

unread,
May 26, 2011, 6:42:25 AM5/26/11
to django...@googlegroups.com
Fix   some_file  = open('bla/bla/bla/', "rw")    to    some_file  = open('bla/bla/bla/', "r")

Fix that "rw" to just "r". You just want the user to read (a.k.a. get) the file, not have write access to it.

Kann

unread,
May 31, 2011, 10:24:01 AM5/31/11
to Django users
Dear all,

I am a bit confused about how to set the MEDIA_URL variable here.
Currently, I am testing the web using Django embedded webserver and
doesn't have a proper url yet. Should my MEDIA_URL be something
like... http://my_machine_name:8000/media/

Kann

Ivo Brodien

unread,
May 31, 2011, 10:44:00 AM5/31/11
to django...@googlegroups.com
Hi did you read this documentation about serving static files?

In generally you don’t want serve files through django, but through your actual webserver (e.g. Apache, nginx, lighttpd...).

However during development you can make django serve them.

See: Serving static files in development

https://docs.djangoproject.com/en/dev/howto/static-files/

Since it can become a bit cumbersome to define this URL pattern, Django ships with a small URL helper function static() that takes as parameters the prefix such as MEDIA_URL and a dotted path to a view, such as 'django.views.static.serve'. Any other function parameter will be transparently passed to the view.
An example for serving MEDIA_URL ('/media/') during development:


from django.conf import settings
from django.conf.urls.static import static

urlpatterns = patterns('',


# ... the rest of your URLconf goes here ...
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

cheers

> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to django-users...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
>

Ivo Brodien

unread,
May 31, 2011, 10:51:03 AM5/31/11
to django...@googlegroups.com

> I am a bit confused about how to set the MEDIA_URL variable here.
> Currently, I am testing the web using Django embedded webserver and
> doesn't have a proper url yet. Should my MEDIA_URL be something
> like... http://my_machine_name:8000/media/


yes, but so in your template you do:

> ### in the template ####


> <a href=“{{ MEDIA_URL }}{{ django_file.url }}>download</a>


Your:


> <a href="file:///{{ django_file.url }}>download</a>

file:/// will only work on you machine, since file:/// is pointing to a local file

Kann

unread,
May 31, 2011, 11:58:23 AM5/31/11
to Django users
Thanks for your help Ivo,

I have another question here. It seems that django.contrib.staticfiles
can be used to handle some static files during the 'debug' mode while
using the embedded 'runserver' from django. However, would it be
possible if I just develop my website using the system's Apache and
not using the django.contrib.staticfiles?

Kann

On May 31, 4:51 pm, Ivo Brodien <i...@brodien.de> wrote:
> > I am a bit confused about how to set the MEDIA_URL variable here.
> > Currently, I am testing the web using Django embedded webserver and
> > doesn't have a proper url yet. Should my MEDIA_URL be something
> > like...http://my_machine_name:8000/media/

Ivo Brodien

unread,
May 31, 2011, 12:17:57 PM5/31/11
to django...@googlegroups.com
you are welcome!

> I have another question here. It seems that django.contrib.staticfiles
> can be used to handle some static files during the 'debug' mode while
> using the embedded 'runserver' from django. However, would it be
> possible if I just develop my website using the system's Apache and
> not using the django.contrib.staticfiles?

Of course and this is even better, because this is how the setup should be on real server.

You have to configure Apache to serve for example everything under http://mydomain.com/static/ as a simple file without talking to django at all. The static stuff is good for static files like CSS and JavaScript.

BTW: although this goes beyond the question I just want to mention it for completeness.

On the other hand the {{MEDIA_URL }} and MEDIA_ROOT is more for server/user generated files. You can still have Apache serve the file after authorizing it against Django. The keyword here is: x-sendfile

http://stackoverflow.com/questions/1156246/having-django-serve-downloadable-files

What you were doing with open the file probably only makes sense if you need to process the contents of the file in any way.


Nishant Boro

unread,
Jun 24, 2019, 9:14:22 AM6/24/19
to Django users

You can use this simple module:

https://github.com/nishant-boro/django-rest-framework-download-expert

This module provides a simple way to serve files for download in django rest framework using Apache module Xsendfile. It also has an additional feature of serving downloads only to users belonging to a particular group

aditya surana

unread,
Jan 23, 2020, 7:26:02 AM1/23/20
to Django users
What if i don't want to add this additional feature?.

Kasper Laudrup

unread,
Jan 23, 2020, 7:47:12 AM1/23/20
to django...@googlegroups.com
Hi Aditya,

On 23/01/2020 12.46, aditya surana wrote:
> What if i don't want to add this additional feature?.
>

Then don't?

Kind regards,

Kasper Laudrup
Reply all
Reply to author
Forward
0 new messages