代码之家  ›  专栏  ›  技术社区  ›  Jorge Guberte

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

  •  0
  • Jorge Guberte  · 技术社区  · 14 年前

    我有一门课没有延伸 webapp.RequestHandler ,我不能使用 self.response.out.write() ,我得到:

    AttributeError:获取程序实例没有属性“response”

    webapp.RequestHandler (我以为会有用),我得到:

    我怎样才能正确地使用这种方法呢?有时 print 也不起作用;我只是得到一个空白屏幕。

    编辑:

    application: fbapp-lotsofquotes
    version: 1
    runtime: python
    api_version: 1
    
    handlers:
    - url: .*
      script: main.py
    

    来源 #<- 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()
    
    1 回复  |  直到 8 年前
        1
  •  2
  •   Matthew Flaschen    14 年前

    你没有被传送到 Fetcher 当您初始化 WSGIApplication request response 财产。您可以从路由到的处理程序中手动执行此操作,例如 MainHandler ViewQuoteHandler 。例如。:

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

    注意,fetcher实际上不必是 RequestHandler