views:

45

answers:

1

Hi.

What's the best way to handle form POST data in my Pylons app? I've tried:

  • Having a seperate GET method and a POST method with a rest.restrict('post') decorator. Problem -- if there were validation errors then you can't redisplay the form with the data which the user entered because you have to redirect back to the GET method OR you have to render the template directly from the POST method. Unfortunately this looks weird, as the URL has to change to correspond to the POST action.

  • Having it all in one method, and detecting if the form has been posted via a check on request.method. This works okay, but it seems clumsy to have if request.method == 'post': ... else: ...

+2  A: 

Having it all in one method, and detecting if the form has been posted via a check on request.method. This works okay, but it seems clumsy to have if request.method == 'post': ... else: ...

I am not sure why you describe this as clumsy. Switching on request method is a valid idiom in the web app world across languages. For e.g. you'll find Django views having a single view that handles requests differently based on request.method. Similarly in Java, Servlets have doPost() and doGet() methods to provide different behavior for GET and POST requests.

Update

I'd just rather have them separated into different methods, if possible. Many other web frameworks do this

Nothing wrong with this approach either. I was merely pointing out that having the same method handle them is equally valid.

Manoj Govindan
I'd just rather have them separated into different methods, if possible. Many other web frameworks do this.
Matt H
@Matt H: Nothing wrong with this approach either. I was merely pointing out that having the same method handle them is equally valid
Manoj Govindan