views:

133

answers:

2

I'm working on a Google App Engine project that collects stories submitted by users.

This is how I handle submission errors in the post method of my Request Handler:

# get the title and content using self.request.get()
errors = []
if not title:
    errors.append("Please enter a title.")
if not content:
    errors.append("Please enter a story.")
if not errors:
    # create the story, save it to the database
    # redirect to the story's page
else:
    # pass the title and/or content to a template
    # pass the error message(s) to a template
    # the same template that displays the submission form is used here

The problem: since my form sends posts to example.com/createstory.do -- if there are errors I end up redisplaying the form page at that address.

What I want to happen: redirect the user back the page where they submitted the form: example.com/Share, while at the same time displaying the error messages and redisplaying the submitted form data.

What's the easiest way to do this?

I know I could just have /Share handle both get and post requests, but I'm looking for a solution that I can use even when doing that wouldn't be an option.

+1  A: 

You could redirect to /Share including the errors in a GET variable in the URL, if you're absolutely sure you need to use separate URLs. Of course, this makes your URL ugly since it now has all of the error information in it.

Another option would be to redirect back to Share and have the errors stored in cookies or in a session variable.

Combining either of these with client-side form validation in Javascript before submitting in the first place so the ugly solution isn't hit in most cases might be the best option.

Wooble
would it be okay to use something like urllib to request a page and set the headers? docs: http://code.google.com/appengine/docs/python/urlfetch/overview.html
wings
+1  A: 

There's no 'clean' way of doing this, because you cannot redirect POST requests (and have the redirected request also make a POST). The standard - and cleanest - approach is to have the same URL display the form when fetched with GET, and accept the data when fetched with POST.

Nick Johnson