免责声明: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/