如何将当前查询字符串添加到Django模板中的URL? [英] How to add the current query string to an URL in a Django template?

查看:95
本文介绍了如何将当前查询字符串添加到Django模板中的URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我加载页面时,有一个链接sameLink,我想追加它的 查询字符串

When I load a page, there is a link "sameLink" that I want to append to it the query string of its containing page.

我有以下URL:

somedomain/reporting/article-by-month?variable1=2008

我该怎么做?

推荐答案

要捕获作为请求的一部分的QUERY_PARAMS,请引用包含这些参数的dict( request.GET ),并对它们进行urlencode编码,以便它们作为href的一部分可以接受。 request.GET.urlencode 返回一个字符串,看起来像 ds =& date_published__year = 2008 ,可以放入链接如下:

To capture the QUERY_PARAMS that were part of the request, you reference the dict that contains those parameters (request.GET) and urlencode them so they are acceptable as part of an href. request.GET.urlencode returns a string that looks like ds=&date_published__year=2008 which you can put into a link on the page like so:

<a href="sameLink/?{{ request.GET.urlencode }}">

这篇关于如何将当前查询字符串添加到Django模板中的URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆