Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 716
interstar Avatar asked Oct 19 '25 12:10

interstar


1 Answers

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.

like image 194
Dominic Rodger Avatar answered Oct 22 '25 01:10

Dominic Rodger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!