python - 如何在重定向后及早关闭 GAE 上的 Python Web 应用程序?

标签 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')

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

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

我想确保一旦在 some_function() 中进行了重定向(工作正常),在重定向之后的 get() 函数中没有进行任何处理,也没有输出“此处的一些输出”。

我应该注意什么才能使这一切正常工作?我不能只退出脚本,因为 webapp 框架需要运行。

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

谢谢

最佳答案

这个怎么样?

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')

作为引用,如果你需要在很多 RequestHandlers 中使用 some_function() ,那么创建一个你的其他 RequestHandlers 可以从中继承的类将是 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')

关于python - 如何在重定向后及早关闭 GAE 上的 Python Web 应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3002233/

相关文章:

python - 查找列表的索引(如果它们超出特定值)。 python ,numpy

java - JayDeBeApi 连接崩溃

python - 发送添加了用户代理的 QHTTP 请求

javascript - 将 CSS/CSS3/JS 链接到 Google App Engine?

php - 如何公开要从 Wordpress 管理面板运行的 Swift 代码(iOS 应用程序),如 php 服务器代码

C# - 如果用户在插入记录后刷新页面,则会创建重复记录。我怎样才能防止这种情况发生?

python - type(x) 是列表 vs type(x) == 列表

python - 如何使用 HTTP header 发送非英文 unicode 字符串?

google-app-engine - 节奏模板引擎和GAE

azure - AADSTS50012 : Invalid client secret is provided when moving from a Test App to Production