On Sat, May 12, 2012 at 07:51:01AM -0700, brian wrote:
> The redirect method isn't working.  I use middleware to insert a form
> into the context for each page.  When I try to redirect with a
> fragment identifier, then the page scrolls but I don't see the form
> errors.  If I remove the error form redirect then I see the errors but
> this removes the scroll.
> 
> Here is my code:
> ---------------------------------------------------------------
[...]
> ---------------------------------------------------------------
> 
> With this, I see the scroll but my form errors aren't being shown.  In
> my form template I have <{% if form.errors %}>.
> 
> If I comment out the line < return HttpResponseRedirect(request.path +
> '#learnMoreId')> and the else before it, then I get the scroll but I
> don't get the form errors.

Well, since HTTP is stateless, errors can only be shown in the exact
response to the request in which the form is submitted. If the request
is GET, obviously the form won't contain any errors.

Now let's look at what happens if a user submits the form to your
view. When the form is invalid, instead of redisplaying it, you return
a redirect response. When the browser gets a redirect, it loads
request.path + "#learnMoreId" with a GET request and the form data is
completely lost.

In other words, only return a redirect on success, not on failure.

You might have more luck setting the "action" attribute in your HTML
form to "#learnMoreId", though I'm not sure this will work; you'll
have to try that yourself.

Michal

Attachment: signature.asc
Description: Digital signature

Reply via email to