我想进行重定向并保留查询字符串。像self.redirect
这样的东西加上发送的查询参数。这可能吗?
答案 0 :(得分:18)
newurl = '/my/new/route?' + urllib.urlencode(self.request.params)
self.redirect(newurl)
答案 1 :(得分:10)
您可以使用self.request.query_string
将查询字符串提取到当前请求;因此,您可以使用self.redirect('/new/url?' + self.request.query_string)
重定向到新网址。
答案 2 :(得分:1)
这在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()) # or request.GET.urlencode()
new_url = '/my/new/route' + '?' + query_string
请参见https://docs.djangoproject.com/en/2.2/ref/request-response/。
答案 3 :(得分:0)
使用RedirectView
。
from django.views.generic.base import RedirectView
path('go-to-django/', RedirectView.as_view(url='https://djangoproject.com', query_string=True), name='go-to-django')