3

私はグーグルのサイトからこの例をたどってきましたが、根本的なことがどのように機能するかを理解するのに苦労しています。ほとんどの場合、MainHandler HTML でテキストを送信するとき、GuestBook の使用をどのように認識しますか? <form action="/sign" method=post>and と関係があると思いますが、('/sign', GuestBook)すべてがどのように機能するかは完全にはわかりません。

from google.appengine.ext import db
import webapp2

class Greeting(db.Model):
    content = db.StringProperty(multiline=True)
    date = db.DateTimeProperty(auto_now_add=True)

class MainHandler(webapp2.RequestHandler):
    def get(self):
        self.response.write('Hello world!')
        self.response.write('<h1>My GuestBook</h1><ol>')
        #greetings = db.GqlQuery("SELECT * FROM Greeting")
        greetings = Greeting.all()
        for greeting in greetings:
            self.response.write('<li> %s' % greeting.content)
        self.response.write('''
            </ol><hr>
            <form action="/sign" method=post>
            <textarea name=content rows=3 cols=60></textarea>
            <br><input type=submit value="Sign Guestbook">
            </form>
        ''')

class GuestBook(webapp2.RequestHandler):
    def post(self):
        greeting = Greeting()
        greeting.content = self.request.get('content')
        greeting.put()
        self.redirect('/')

app = webapp2.WSGIApplication([
    ('/', MainHandler),
    ('/sign', GuestBook),
], debug=True)
4

1 に答える 1

2

あなたは正しいです!ルートは次のブロックで構成されます。

app = webapp2.WSGIApplication([
    ('/', MainHandler),
    ('/sign', GuestBook),
], debug=True)

So when there is a request to /sign, a new GuestBook instance is created, and the appropriate method is called with the GuestBook instance (which contains a reference to the request) as the first argument. In your example, it is a POST, but webapp2 supports all of the popular http methods as documented at http://webapp-improved.appspot.com/guide/handlers.html

于 2013-05-21T02:09:15.387 に答える