如何在重定向后尽早终止GAE上的Python Web应用程序?

Mik*_*ike 8 python google-app-engine web-applications

免责声明:PHP背景下的全新Python

好吧我在Google App Engine上使用Python和Google的webapp框架.

我有一个导入的功能,因为它包含需要在每个页面上处理的东西.

def some_function(self):
    if data['user'].new_user and not self.request.path == '/main/new':
        self.redirect('/main/new')
Run Code Online (Sandbox Code Playgroud)

当我调用它时,这很好用,但是如何在重定向后确保应用程序被终止.我不想要任何其他处理.例如,我会这样做:

class Dashboard(webapp.RequestHandler):
    def get(self):
        some_function(self)
        #Continue with normal code here
        self.response.out.write('Some output here')
Run Code Online (Sandbox Code Playgroud)

我想确保一旦在some_function()中进行重定向(工作正常),重定向后的get()函数中不进行任何处理,也不输出"Some output here".

我应该怎么看才能使这一切正常运作?我不能退出脚本,因为webapp框架需要运行.

我意识到我很可能只是以完全错误的方式为Python应用程序做任何事情,因此任何指导都将是一个很大的帮助.希望我已经正确地解释了自己,并且有人能够指出我正确的方向.

谢谢

Dan*_*all 4

这个怎么样?

class Dashboard(webapp.RequestHandler):
    def some_function(self):
        if data['user'].new_user and not self.request.path == '/main/new':
            self.redirect('/main/new')
            return True
        else:
            return False
    def get(self):
        if not self.some_function():
            self.response.out.write('Some output here')
Run Code Online (Sandbox Code Playgroud)

作为参考,如果您需要在许多 RequestHandler 中使用 some_function() ,那么创建一个您的其他 RequestHandler 可以继承的类将是 Pythonic 的:

class BaseHandler(webapp.RequestHandler):
    def some_function(self):
        if data['user'].new_user and not self.request.path == '/main/new':
            self.redirect('/main/new')
            return False
        else:
            return True

class Dashboard(BaseHandler):
    def get(self):
        if not self.some_function():
            self.response.out.write('Some output here')
Run Code Online (Sandbox Code Playgroud)