Shan
Shan

Reputation: 1138

Is it possible to use the same method for both get and post with webapp2?

There is very little difference in my get and post methods. One way to do this would be to put the common logic in another function and call that in both the get and post methods. But before I do that I wanted to know If I can actually have one function handle both, that'll be really neat.

Upvotes: 0

Views: 578

Answers (2)

voscausa
voscausa

Reputation: 11706

You can also use a BaseHandler for your handlers. You can put common methods for sessions, login and templates in the BaseHandler.

See this example for sessions or this blog post about webapp2 and templates.

Upvotes: 3

user3058197
user3058197

Reputation: 1052

This is a good description of when to use GET vs POST. You can use either, of course, but there are situations where you'd want to use one vs the other. You can use the same methods to process them from within the same class if you wanted to like this:

class MyHandler(webapp2.RequestHandler): 

    def function_to_handle_requests(self):
        # code goes here

    def get(self): 
        self.function_to_handle_requests

    def post(self):
        self.function_to_handle_requests

Upvotes: 2

Related Questions