jua*_*ren 47 django redirect django-urls django-views
可以在重定向中添加GET变量吗?(无需修改我的urls.py)
如果我做 redirect('url-name', x)
我明白了 HttpResponseRedirect('/my_long_url/%s/', x)
我没有抱怨使用HttpResponseRedirect('/my_long_url/%s/?q=something', x),但只是想知道......
Smi*_*ris 108
由于重定向只返回一个HttpResponseRedirect对象,你可以改变它:
response = redirect('url-name', x)
response['Location'] += '?your=querystring'
return response
Run Code Online (Sandbox Code Playgroud)
Man*_*dan 43
可以在重定向中添加GET变量吗?(无需修改我的urls.py)
如果不修改,我不知道有什么方法可以做到这一点urls.py.
我没有抱怨使用HttpResponseRedirect('/ my_long_url /%s /?q = something',x)而只是想知道......
您可能希望编写一个瘦包装器以使其更容易.说,custom_redirect
def custom_redirect(url_name, *args, **kwargs):
from django.core.urlresolvers import reverse
import urllib
url = reverse(url_name, args = args)
params = urllib.urlencode(kwargs)
return HttpResponseRedirect(url + "?%s" % params)
Run Code Online (Sandbox Code Playgroud)
然后可以从您的视图中调用它.例如
return custom_redirect('url-name', x, q = 'something')
# Should redirect to '/my_long_url/x/?q=something'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
23801 次 |
| 最近记录: |