Django urldecode in template file

守給你的承諾、 提交于 2020-01-13 09:33:05

问题


is there any way do the urldecode in Django template file?

Just opposite to urlencode or escape

I want to convert app%20llc to app llc


回答1:


You could create a simple custom filter around urllib.unquote

For instance:

from django.template.defaultfilters import stringfilter
from urllib import unquote

@stringfilter
def unquote_raw(value):
    return unquote(value)

and now you can have this in your django template file:

{{ raw|unquote_raw }}



回答2:


you have to write something like this instead of the previous answer otherwise you will get a maximum recursion depth

from urllib import unquote
from django.template.defaultfilters import register
from urllib.parse import unquote #python3

@register.filter
def unquote_new(value):
    return unquote(value)

{{ raw|unquote_new }}



来源:https://stackoverflow.com/questions/5229054/django-urldecode-in-template-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!