开发者

How to access a database initialized in a class from another class in python?

How would I go about implementing the MainPage class?

import cgi
import os
import logging

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):  #setting up database
  a = db.StringProperty(multiline=True)
  b = db.StringProperty(multiline=True)
  c = db.StringProperty(multiline=True)
  d = db.StringProperty(multiline=True)开发者_如何学编程

class MainPage(webapp.RequestHandler):
  def get(self):
##I want to access the content of the database here which is defined in Downloader class. 
##For example let the value of greeting.a be assigned to alpha variable.   


#Entering data to the database
class Downloader(webapp.RequestHandler):
    def get(self):
        greeting=Greeting()
        greeting.a=self.request.GET.get('a')
        greeting.b=self.request.GET.get('b')
        greeting.c=self.request.GET.get('c')
        greeting.d=self.request.GET.get('d')
        greeting.put()


You could pass a Downloader object to MainPage on initialization:

class MainPage(webapp.RequestHandler):
    def __init__(self, downloader):
        self.downloader = downloader

    def get(self):
        self.downloader.get()


This is solved by basically naming the database, and refer to it from another class

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜