【问题标题】:What is going on with this code from the Google App Engine tutorialGoogle App Engine 教程中的这段代码是怎么回事
【发布时间】:2009-09-24 08:25:43
【问题描述】:
import cgi

from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db

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

class MainPage(webapp.RequestHandler):
  def get(self):
    self.response.out.write('<html><body>')

    greetings = db.GqlQuery("SELECT * FROM Greeting ORDER BY date DESC LIMIT 10")

    for greeting in greetings:
      if greeting.author:
        self.response.out.write('<b>%s</b> wrote:' % greeting.author.nickname())
      else:
        self.response.out.write('An anonymous person wrote:')
      self.response.out.write('<blockquote>%s</blockquote>' %
                              cgi.escape(greeting.content))

    # Write the submission form and the footer of the page
    self.response.out.write("""
          <form action="/sign" method="post">
            <div><textarea name="content" rows="3" cols="60"></textarea></div>
            <div><input type="submit" value="Sign Guestbook"></div>
          </form>
        </body>
      </html>""")

class Guestbook(webapp.RequestHandler):
  def post(self):
    greeting = Greeting()

    if users.get_current_user():
      greeting.author = users.get_current_user()

    greeting.content = self.request.get('content')
    greeting.put()
    self.redirect('/')

application = webapp.WSGIApplication(
                                     [('/', MainPage),
                                      ('/sign', Guestbook)],
                                     debug=True)

def main():
  run_wsgi_app(application)

if __name__ == "__main__":
  main()

我是 Python 新手,看着这个 Google App Engine 教程代码有点困惑。在 Greeting 类中,content = db.StringProperty(multiline=True),但在 Guestbook 类中,greeting 对象中的“content”然后设置为 greeting.content = self.request.get('content')。

我不明白“内容”变量是如何在 Greeting 类和 Guestbook 类中设置的,但似乎包含两个语句的值和属性。

【问题讨论】:

    标签: python google-app-engine


    【解决方案1】:

    第一段代码是模型定义:

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

    它说有一个模型 Greeting 有一个名为 contentStringProperty

    在第二段代码中,您创建了Greeting 模型的实例并为其content 属性赋值

    greeting = Greeting()
    greeting.content = self.request.get('content')
    

    编辑:在评论中回答您的问题:这是基本的面向对象编程(或 OOP),带有一点 Python 的特殊酱汁(描述符和元类)。如果您是 OOP 新手,请阅读 this article 以更熟悉该概念(这是一个复杂的主题,OOP 上有完整的库,所以除了阅读一篇文章后,别无所求)。您实际上不必知道描述符或元类,但它有时会派上用场。这是描述符的good introduction

    【讨论】:

    • 感谢 Piquadrat 花一点时间回答我的问题。
    • 所以变量“content”不仅仅是一个变量,而是实际上将一个StringProperty命名为“content”?这是python可以做的一些我没有得到的奇怪事情吗?
    • 是的描述符!!!提供的关于它们的链接非常完美,现在一切都变得更有意义了。多么酷的概念,你在 Java 中看不到这样的东西。
    【解决方案2】:
    class Greeting(db.Model):
      author = db.UserProperty()
      content = db.StringProperty(multiline=True)
      date = db.DateTimeProperty(auto_now_add=True)
    

    此代码指示 ORM(对象关系映射器)在数据库中创建一个包含“作者”、“内容”和“日期”字段的表。请注意 Greeting 类是如何从 db.Model 继承的:它是在数据库中创建表的模型。

    class Guestbook(webapp.RequestHandler):
      def post(self):
        greeting = Greeting()
    
        if users.get_current_user():
          greeting.author = users.get_current_user()
    
        greeting.content = self.request.get('content')
        greeting.put()
        self.redirect('/')
    

    Guestbook 是一个请求处理程序(注意它是从哪个类继承的)。请求处理程序的 post() 方法在 POST 请求事件上被调用。此类中还可以有几种其他方法来处理不同类型的请求。现在注意 post 方法的作用:它实例化 Greeting 类——我们现在有一个实例,greeting 对象。接下来,根据请求信息设置问候对象的“作者”和“内容”。最后,greeting.put() 写入数据库。此外,请注意“日期”也会自动设置为将对象写入数据库的日期/时间。

    【讨论】:

      【解决方案3】:

      piquadrat 的回答很好。您可以阅读有关 App Engine 模型的更多信息here

      【讨论】:

        猜你喜欢
        • 2018-11-30
        • 2018-06-20
        • 2022-07-18
        • 2013-09-23
        • 2023-03-30
        • 2013-10-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多