Django : How do I call "reverse" on a link to a static image file?

django, django-templates

Solution

It's probably easier to just use:

{{ MEDIA_URL }}somepath/image.jpg

assuming you've set up the `MEDIA_URL` setting in your `settings.py`.

Also, you really don't want to serve static files through Django in a production environment. From the docs:

Using this method is inefficient and insecure. Do not use this in a production setting. Use this only for development.

One more thing - what is `staticfiles.views.serve`? Django has a built in system for serving static files, though the disclaimer above still applies.

You probably want to serve static files straight from Apache/nginx/whatever. It'll be heaps quicker.

Problem

In Django, I have images stored in site_media like this : ``` /site_media/somepath/image.jpg ``` The urls.py file has been set up to server these using : ``` urlpatterns += patterns('', (r'^site_media/(?P<path>.*)$', 'staticfiles.views.serve') ) urlpatterns += patterns('', (r'^site_media/(?P<path>.*)$', 'staticfiles.views.serve') ) ``` So how can I make a reverse() or {% url %} call to one of these images?

Original source