Passing arguments on redirect (Django 1.1)

2,159 views
Skip to first unread message

Matt

unread,
Sep 30, 2009, 12:05:33 PM9/30/09
to Django users
I'm using the redirect() function provided in Django 1.1:
http://docs.djangoproject.com/en/dev/topics/http/shortcuts/#django.shortcuts.redirect

I have a view:
my_editing_view(request, id, args={})

for which the URL is:
url(r'^/edit/(?P<id>.+)$', 'views.my_editing_view')

And I'd like to redirect to that view from another view such that I
set BOTH id and args.

That is:
some_id = ...
some_args = ...
redirect('my_editing_view', id=some_id, args=some_args)

I'm able to do the following:
redirect('my_editing_view', id=some_id)

Is there a way to pass these extra args without having to include them
in the URL? Sorry if this is basic, but I'm new to Django.

Thanks,
Matt

Daniel Roseman

unread,
Sep 30, 2009, 12:53:39 PM9/30/09
to Django users
On Sep 30, 5:05 pm, Matt <mattsp...@gmail.com> wrote:
> I'm using the redirect() function provided in Django 1.1:http://docs.djangoproject.com/en/dev/topics/http/shortcuts/#django.sh...
>
> I have a view:
> my_editing_view(request, id, args={})
>
> for which the URL is:
> url(r'^/edit/(?P<id>.+)$', 'views.my_editing_view')
>
> And I'd like to redirect to that view from another view such that I
> set BOTH id and args.
>
> That is:
> some_id = ...
> some_args = ...
> redirect('my_editing_view', id=some_id, args=some_args)
>
> I'm able to do the following:
> redirect('my_editing_view', id=some_id)
>
> Is there a way to pass these extra args without having to include them
> in the URL?  Sorry if this is basic, but I'm new to Django.
>
> Thanks,
> Matt

But what is 'args'? You're not including it in the urlpattern, so how
can it be passed to the view? If you mean as querystring parameters
(eg /myview/?x=1&y=2), these aren't passed as arguments to the view at
all, but are accessed via request.GET - you do have to add these on to
the URL manually, but can easily convert them from a dictionary via
django.utils.http,urlencode,

(Not related, but note that you should never use a dict or a list as a
default argument in a function definition: see
http://www.ferg.org/projects/python_gotchas.html#contents_item_6 for
why.)
--
DR.

Matt

unread,
Sep 30, 2009, 2:43:50 PM9/30/09
to Django users
No, that's a good point. You fundamentally can't give someone a
forced POST request on a redirect (essentially what I was trying to
do) through Django because That's Not How the Internet Works (TM).

I just made the save() function be called by an ajax request, and if
there are errors, the display is handled in JS (not through another
template).

Thanks,
Matt
Reply all
Reply to author
Forward
0 new messages