Loosing GET parameters while redirecting with generic view

1,227 views
Skip to first unread message

Filipe Correia

unread,
Jan 2, 2009, 9:25:26 AM1/2/09
to Django users
Hi all,

I'm using a generic view to redirect from one URL to another:

urlpatterns += patterns('prj.app.views',
(r'^foo/(?P<tail>.*)$', redirect_to, {'url': '/foo/bar/%(tail)s'}),
)

However, the URLs I want to redirect actually look like this:

http://localhost/foo/?param1=value1&param2=value2

This means that when redirecting the GET parameters will be lost,
which is not my intent.

Is there any way to maintain GET parameters with the "redirect_to"
generic view? Or will I have to implement my own view?

Thanks in advance.

Cheers,
Filipe

Rajesh Dhawan

unread,
Jan 2, 2009, 9:40:04 AM1/2/09
to Django users
Hi,
The built-in "redirect_to" view doesn't support that. You will have to
implement your own.

-Rajesh D

bruno desthuilliers

unread,
Jan 2, 2009, 10:18:03 AM1/2/09
to Django users


On 2 jan, 15:25, Filipe Correia <fcorr...@gmail.com> wrote:
> Hi all,
>
> I'm using a generic view to redirect from one URL to another:
>
> urlpatterns += patterns('prj.app.views',
> (r'^foo/(?P<tail>.*)$', redirect_to, {'url': '/foo/bar/%(tail)s'}),
> )
>
> However, the URLs I want to redirect actually look like this:
>
> http://localhost/foo/?param1=value1&param2=value2

The url dispatch is made on the request "path" component, which
doesn't include GET params.

> This means that when redirecting the GET parameters will be lost,
> which is not my intent.
>
> Is there any way to maintain GET parameters with the "redirect_to"
> generic view? Or will I have to implement my own view?

The second. But this shouldn't be that hard:

# NB : Q&D, not tested

def my_redirect_to(request, url):
qs = request.META.QUERY_STRING
if qs:
url = "%s?%s" % (url, qs)
return redirect_to(url)

HTH

Filipe Correia

unread,
Jan 2, 2009, 11:37:12 AM1/2/09
to Django users
Thanks for the replies Rajesh and Bruno, they were very helpful!

On Jan 2, 3:18 pm, bruno desthuilliers <bruno.desthuilli...@gmail.com>
wrote:
> # NB : Q&D, not tested
> def my_redirect_to(request, url):
>    qs = request.META.QUERY_STRING
>    if qs:
>       url = "%s?%s" % (url, qs)
>    return redirect_to(url)

Here's the solution I ended up with (it also forwards keyword args):

def redirect_to_with_query(request, url, **kwargs):
query_string = request.META["QUERY_STRING"]
if query_string:
url = "%s?%s" % (url, query_string)
return redirect_to(request, url, **kwargs)


Cheers,
Filipe

bruno desthuilliers

unread,
Jan 2, 2009, 12:40:43 PM1/2/09
to Django users
On 2 jan, 17:37, Filipe Correia <fcorr...@gmail.com> wrote:
> Thanks for the replies Rajesh and Bruno, they were very helpful!
>
> On Jan 2, 3:18 pm, bruno desthuilliers <bruno.desthuilli...@gmail.com>
> wrote:
(snip)
> Here's the solution I ended up with (it also forwards keyword args):
>
> def redirect_to_with_query(request, url, **kwargs):
> query_string = request.META["QUERY_STRING"]

Duh :(

I knew there was something wrong. Thanks for posting the
correction !-)

Reply all
Reply to author
Forward
0 new messages