使用django.shortcuts.redirect添加request.GET变量 [英] add request.GET variable using django.shortcuts.redirect
问题描述
可以在重定向中添加GET变量吗? (不需要修改我的urls.py)
Is possible to add GET variables in a redirect ? (Without having to modifiy my urls.py)
如果我做 redirect('url-name',x)
我得到 HttpResponseRedirect('/ my_long_url /%s /',x)
我没有抱怨使用 HttpResponseRedirect('/ my_long_url /%s /?q = something',x)
想知道...
I don't have complains using HttpResponseRedirect('/my_long_url/%s/?q=something', x)
instead, but just wondering...
推荐答案
可以在重定向中添加GET变量吗? (无需修改我的urls.py)
Is possible to add GET variables in a redirect ? (Without having to modifiy my urls.py)
我不知道有没有办法这样做没有修改 urls.py
。
I don't know of any way to do this without modifying the urls.py
.
我没有抱怨使用HttpResponseRedirect('/ my_long_url /%s /?q = something',x)而是只是想知道...
I don't have complains using HttpResponseRedirect('/my_long_url/%s/?q=something', x) instead, but just wondering...
想写一个薄薄的包装,使这更容易。说, custom_redirect
You might want to write a thin wrapper to make this easier. Say, 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)
然后可以从您的观点。例如
This can then be called from your views. For e.g.
return custom_redirect('url-name', x, q = 'something')
# Should redirect to '/my_long_url/x/?q=something'
这篇关于使用django.shortcuts.redirect添加request.GET变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!