一组用于附加、删除或替换URL中的查询字符串参数的工具(最初由SophiLabs创建)
django-qurl的Python项目详细描述
从URL中追加、删除或替换查询字符串参数(保留顺序)。 最初由Sophilabs创建-https://github.com/sophilabs/django-qurl-templatetag
安装
pip install -e git+https://github.com/obiwanus/django-qurl.git#egg=django-qurl
模板中的用法
示例:
{% load qurl %} {% qurl '/search?page=1&color=blue&color=green' order=name page-- color+=red color-=green %} Output: /search?color=blue&order=name&color=red {% qurl request.get_full_path order='name' %} Output: /your/current/path/?order=name
参数:
name=value: replace all values of name by one value name--: remove all values of name name+=value: append a new value for name name-=value: remove the value of name with the value
在模板标记中使用“反转”:
{% qurl 'url_name' [reverse_params] | order=name color+=red color+=green %} Output: /reversed/url/?order=name&color=red&color=green
相反的语法与标准django^{tt1}中的语法完全相同$ 标签。
视图中的用法
示例:
fromqurlimportqurlurl='/testurl/1/'qurl(url,add={'a':'a1','b':['b1','b2']})# Output: /testurl/1/?a=a1&b=b1&b=b2url='/testurl/1/?a=a1&b=b1&b=b2'qurl(url,add={'a':'a2'},remove=['b'])# Output: /testurl/1/?a=a2url='/testurl/1/?a=a1&b=b1&b=b2'qurl(url,add={'a':['a2']},remove=['b'])# Output: /testurl/1/?a=a1&a=a2url='/testurl/1/?a=a1&b=b1&b=b2'qurl(url,exclude={'b':'b2'},add={'a':'a2'})# Output: /testurl/1/?a=a1&a=a2&b=b1# You can use request.GET as a baseurl='/testurl/1/'qurl(url,add=request.GET,exclude={'b':'b1'}
注:
如果要为参数指定特定值(全部替换 现有值,使用^ {TT2}$。
如果要向已存在的值添加值,请使用 add={'param':['value']}。当然,您可以添加多个值。
请检查测试以获取更多使用示例。