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