Disabling PRG Pattern

65 views
Skip to first unread message

Chris

unread,
Apr 26, 2009, 7:39:33 PM4/26/09
to Django users
I've noticed a Redirect/Get is issued automatically after every Post
made. Is there any way to disable this feature? I can't find anything
in the docs about this.

Regards,
Chris

Chris

unread,
Apr 26, 2009, 7:39:34 PM4/26/09
to Django users

Alex Gaynor

unread,
Apr 26, 2009, 7:40:37 PM4/26/09
to django...@googlegroups.com
Where are you talking about?  No where does django impose upon you a post-redirect-get pattern for your own views, it just happens to be the convention.

Alex

--
"I disapprove of what you say, but I will defend to the death your right to say it." --Voltaire
"The people's good is the highest law."--Cicero

Chris

unread,
Apr 26, 2009, 10:41:51 PM4/26/09
to Django users
On Apr 26, 7:40 pm, Alex Gaynor <alex.gay...@gmail.com> wrote:
> On Sun, Apr 26, 2009 at 7:39 PM, Chris <chriss...@gmail.com> wrote:
>
> > I've noticed a Redirect/Get is issued automatically after every Post
> > made. Is there any way to disable this feature? I can't find anything
> > in the docs about this.
>
> > Regards,
> > Chris
>
> Where are you talking about?  No where does django impose upon you a
> post-redirect-get pattern for your own views, it just happens to be the
> convention.

My view returns a normal HTML response by default, and a JSON response
for Ajax requests. So it looks like:

if request.is_ajax():
return JSONResponse({'response_value': 123})
else:
response = render_to_response('my_page.html',
locals(),
context_instance=RequestContext
(request),
)
response.set_cookie('my_cookie', 123)
return response

And for either method, if it's a form post, I get a 302 redirect, and
I really have no idea why. It's driving me nuts.

Chris

Doug B

unread,
Apr 26, 2009, 10:56:12 PM4/26/09
to Django users
Maybe the APPEND_SLASH setting is causing it (along with a missing
slash at the end of your url)?

Chris Spencer

unread,
Apr 26, 2009, 11:01:52 PM4/26/09
to django...@googlegroups.com
On Sun, Apr 26, 2009 at 10:56 PM, Doug B <dbal...@gmail.com> wrote:
>
> Maybe the APPEND_SLASH setting is causing it (along with a missing
> slash at the end of your url)?

I've made sure to append the slash to all my urls. Firebug confirms my
ajax requests are using the slash-appended url, and logging confirms
it's hitting my view twice. Wouldn't the slash append cause the
redirect before it ever got to my view, causing my view to only get
called once?

Chris

Malcolm Tredinnick

unread,
Apr 26, 2009, 11:26:14 PM4/26/09
to django...@googlegroups.com

There's nothing in that code fragment that handles POSTs specifically.
Can you provide a more self-contained fragment. What does the URLConf
entry look like that will be used to hand things to a simplified view
that shows the behaviour you're seeing? If we could see the URL Conf
entry and the entire view (simplified to remove everything but the
minimal information), that would certainly help.

I will confirm what Alex mentioned, however, Django does not, by default
do anything that you've described. There is the CommonMiddleware
behaviour of redirecting to a slash appended URL, but you've mentioned
that you've ruled that out. I can't think of any other middleware that
might be doing what you've suggested, but if you've added any middleware
that would be worth mentioning as well.

Also, does the code execution get to your view before you're seeing this
redirect? Put some debugging prints in to see whether this is happening.

Regards,
Malcolm


Chris Spencer

unread,
Apr 27, 2009, 8:21:40 AM4/27/09
to django...@googlegroups.com

Sorry, this was completely my fault. Turns out I had accidentally
placed my submit
button inside an achor tag. Removing that's fixed the problem.

Thanks for the help.

Chris

Reply all
Reply to author
Forward
0 new messages