我想进行重定向并保留查询字符串是什么。类似于self.redirect
加上已发送的查询参数。有可能吗?
newurl = '/my/new/route?' + urllib.urlencode(self.request.params)
self.redirect(newurl)
您可以使用self.request.query_string
将查询字符串提取到当前请求;因此,您可以使用self.redirect('/new/url?' + self.request.query_string)
重定向到新的URL。
这在Django 2.2中为我工作。查询字符串可用作HTTP GET的QueryDict实例request.GET
和HTTP POST的request.POST
。将它们转换为普通词典,然后使用urlencode
。
from django.utils.http import urlencode
query_string = urlencode(request.GET.dict())
new_url = '/my/new/route' + '?' + query_string
请参见https://docs.djangoproject.com/en/2.2/ref/request-response/。