Django - urls.py - 文件名中有井号 (#)?
我在使用Django的时候发现,如果用户想要访问的文件名(比如一张照片)里面有井号(#),那么在url.py里的设置就无法匹配到这个文件。
有没有什么好主意呢?
url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root':
MEDIA_ROOT},
它只是显示:
"/home/user/project/static/upload/images/hello" does not exist
但实际上文件的名字是:
hello#world.jpg
谢谢,
Nico
1 个回答
11
这其实不是Django的问题。在网址中,井号(#)的意思是要加载页面上的某个特定位置。你需要对网址中的井号进行编码,这样浏览器才能请求到完整的图片路径:
/home/user/project/static/upload/images/hello%23world.jpg
在Django的模板中,你可以使用urlencode这个模板标签。