1

我遵循了 Google App Engine 教程,但在向留言簿类中的响应对象添加内容时遇到了一些麻烦。

class Guestbook(webapp2.RequestHandler):
def post(self):

    # We set the same  parent key on the 'Greeting' to ensure each greeting
    # is in the same entity group. Queries across the single entity group
    # will be consistent. However, the write rate to a single entity group
    # should be limited to ~1/second.
    guestbook_name = self.request.get('guestbook_name',
                                      DEFAULT_GUESTBOOK_NAME)
    testvar = self.request.get('testvar',
                                      DEFAULT_GUESTBOOK_NAME)
    greeting = Greeting(parent=guestbook_key(guestbook_name))

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

    greeting.content = self.request.get('content')
    greeting.info = 'DIDTHISWORK?'
    greeting.put()

    self.response.headers.add_header("Expires", 'Information here')      
    #self.response.set_status(200,'Is this working?!')

    self.response.headers['Content-Type'] = 'text/plain'
    #self.response.headers['Content-Length'] = '5'
    self.response.out.write('Hello')


    query_params = {'guestbook_name': guestbook_name}
    self.redirect('/?' + urllib.urlencode(query_params))
    print type(self.response)

这里使用 Wireshark 是响应数据包:

HTTP/1.1 302 Found
Cache-Control: no-cache
Expires: Information here
Content-Type: text/plain
Location: http://_______.appspot.com/?guestbook_name=default_guestbook
Date: Fri, 17 May 2013 01:21:52 GMT
Server: Google Frontend
Content-Length: 0

如您所见,我正在尝试用“Hello”填充内容正文,但它一直给我 content-length = 0 并手动设置它似乎没有帮助,所以我将其注释掉。我认为您可以放心地忽略带有问候语的代码,但我在其中添加了它以防它影响我所做的任何事情。

4

1 回答 1

2

您在将任何内容打印到页面之前发送重定向,因此长度为 0。

self.redirect('/?' + urllib.urlencode(query_params)) # redirects
print type(self.response)                            # never executes

如果您查看 Wireshark 捕获,请查看 302 重定向的响应代码和Location:告诉浏览器重定向到何处的标头。

HTTP/1.1 302 Found 
Location: http://_______.appspot.com/?guestbook_name=default_guestbook
于 2013-05-17T01:49:52.543 回答