Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - urls.py - Filenames with a hash/pound (#) sign?

I'm using django and realized that when the filename that the user wants to access (let's say a photo) has the pound sign, the entry in the url.py does not match.

Any ideas?

    url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root':
    MEDIA_ROOT},

it just says:

"/home/user/project/static/upload/images/hello" does not exist

when actually the name of the file is:

hello#world.jpg

Thanks, Nico

like image 776
miya Avatar asked Dec 28 '22 21:12

miya


1 Answers

This isn't really Django's fault - the pound (#) sign in a URL means to load the specified anchor on the page. You need to encode the pound sign in your URL to get the browser to request the full image path:

/home/user/project/static/upload/images/hello%23world.jpg

In a Django template you can use the urlencode template tag.

like image 161
Lance McNearney Avatar answered Dec 31 '22 10:12

Lance McNearney