简体   繁体   English

self.response.out.write() - 如何正确使用它?

[英]self.response.out.write() - how to use it properly?

I have a class that doesn't extend webapp.RequestHandler , and I can't use self.response.out.write() , I get: 我有一个不扩展webapp.RequestHandler的类,并且我不能使用self.response.out.write() ,我得到:

AttributeError: Fetcher instance has no attribute 'response' AttributeError:Fetcher实例没有属性“响应”

If I extend webapp.RequestHandler (I thought it would work), I get: 如果我扩展webapp.RequestHandler (我认为它会工作),我得到:

AttributeError: 'Fetcher' object has no attribute 'response' AttributeError:“ Fetcher”对象没有属性“ response”

How can I use that method properly? 我该如何正确使用该方法? Sometimes print doesn't work either; 有时print也不起作用; I just get a blank screen. 我只是得到一个空白的屏幕。

EDIT: 编辑:

app.yaml: app.yaml中:

application: fbapp-lotsofquotes
version: 1
runtime: python
api_version: 1

handlers:
- url: .*
  script: main.py

source (the problematic line is marked with #<- HERE ): 来源 (有问题的行标有#<- HERE ):

import random
import os

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

import facebook


class Quote(db.Model):
    author = db.StringProperty()
    string = db.StringProperty()
    categories = db.StringListProperty()
    #rating = db.RatingProperty()


class Fetcher(webapp.RequestHandler):
    '''
    Memcache keys: all_quotes
    '''

    def is_cached(self, key):
        self.fetched = memcache.get(key)
        if self.fetched:
            print 'ok'#return True
        else:
            print 'not ok'#return False


    #TODO: Use filters!
    def fetch_quotes(self):
        quotes = memcache.get('all_quotes')
        if not quotes:
            #Fetch and cache it, since it's not in the memcache.
            quotes = Quote.all()
            memcache.set('all_quotes',quotes,3600)
        return quotes

    def fetch_quote_by_id(self, id):
        self.response.out.write(id) #<---------- HERE


class MainHandler(webapp.RequestHandler):

    def get(self):
        quotes = Fetcher().fetch_quotes()
        template_data = {'quotes':quotes}
        template_path = 'many.html'
        self.response.out.write(template.render(template_path, template_data))


class ViewQuoteHandler(webapp.RequestHandler):

    def get(self, obj):
        self.response.out.write('viewing quote<br/>\n')
        if obj == 'all':
            quotes = Fetcher().fetch_quotes()
            self.render('view_many.html',quotes=quotes)
        else:
            quotes = Fetcher().fetch_quote_by_id(obj)
            '''for quote in quotes:
                print quote.author
                print quote.'''


    def render(self, type, **kwargs):
        if type == 'single':
            template_values = {'quote':kwargs['quote']}
            template_path = 'single_quote.html'
        elif type == 'many':
            print 'many'

        self.response.out.write(template.render(template_path, template_values))


'''
CREATORS
'''
class NewQuoteHandler(webapp.RequestHandler):

    def get(self, action):
        if action == 'compose':
            self.composer()
        elif action == 'do':
            print 'hi'

    def composer(self):
        template_path = 'quote_composer.html'
        template_values = ''
        self.response.out.write(template.render(template_path,template_values))

    def post(self, action):
        author = self.request.get('quote_author')
        string = self.request.get('quote_string')
        print author, string

        if not author or not string:
            print 'REDIRECT'

        quote = Quote()
        quote.author = author
        quote.string = string
        quote.categories = []
        quote.put()


def main():
    application = webapp.WSGIApplication([('/', MainHandler),
                                          (r'/view/quote/(.*)',ViewQuoteHandler),
                                          (r'/new/quote/(.*)',NewQuoteHandler) ],
                                         debug=True)
    util.run_wsgi_app(application)


if __name__ == '__main__':
    main()

You're not routing to Fetcher when you initialize a WSGIApplication . 初始化WSGIApplication时,您不会路由到Fetcher Rather, you create an instance manually in other handlers. 相反,您可以在其他处理程序中手动创建实例。 Thus, App Engine will not initialize your request and response properties. 因此,App Engine不会初始化您的requestresponse属性。 You can manually do so in from the handlers you route to, such as MainHandler and ViewQuoteHandler . 您可以从路由到的处理程序(例如MainHandlerViewQuoteHandler Eg: 例如:

fetcher = Fetcher()
fetcher.initialize(self.request, self.response)
quotes = fetcher.fetch_quotes()

Note that fetcher really doesn't have to be a RequestHandler . 请注意,fetcher实际上不必是RequestHandler It could be a separate class or function. 它可以是单独的类或函数。 Once you have request and response objects, you can pass them around as you choose. 一旦有了请求和响应对象,就可以根据需要传递它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM