使用 django.shortcuts.redirect 添加 request.GET 变量 [英] add request.GET variable using django.shortcuts.redirect

查看:24
本文介绍了使用 django.shortcuts.redirect 添加 request.GET 变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在重定向中添加 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屋!

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