【问题标题】:Call multiple functions from a single route in flask从烧瓶中的单个路由调用多个函数
【发布时间】:2017-03-20 17:53:58
【问题描述】:

我在一个页面方向上有 2 个功能。

如何分别访问它们?
现在发生的事情是,只有第一个函数被调用。

下面的程序是一个虚构的原型,我的要求建立在这个逻辑之上。

from bottle import get,post,run,route,request

content1 = ''' 
<html>
<h1>Page 1 function </h1>
<form action='details' method='post'>
<input type = "text" name="uname">
<input type = "submit" >
</form>
</html>'''

content11 = ''' 
<html>
<h1>Page 2 function </h1>
<form action='details' method='post'>
<input type = "text" name="uname">
<input type = "submit" >
</form>
</html>'''

content2 = '''<html><h1>Hello %s </h1></html>'''

@get('/home')
def page1():
    return content1

def page2():
    return content11  

@post('/details')
def page3():
    u_name = str(request.forms.get('uname'))
    return content2 %u_name

run(host='localhost', port=8080, debug=True)

【问题讨论】:

    标签: python flask bottle


    【解决方案1】:

    您提出问题的方式意味着您希望提供来自同一地址的两个单独的网页。 REST 不能以这种方式工作。

    您需要提供第二条路径来访问page2()中的代码

    【讨论】:

    • 我完全同意你的观点。但是如果我想把类似的安全网页组合在一起,我可以把它放在1个地址下,然后调用需要的函数,这就是为什么我想在1个路由下有多个函数。
    • 您可以使用路由变量来确定要呈现哪个视图,或者您需要在程序中使用全局变量来跟踪它,然后让单个路由函数根据该变量返回任一页面。
    • 我同意你的观点,但你能分享一个例子吗?
    • 解决此问题的最佳方法是了解用于服务content1content11 的逻辑。如果您必须检查用户是否通过其他系统登录,则可以在 page1 函数中使用简单的 if/then 语句。如果是登录,像 Canister (github.com/dagnelies/canister) 这样的东西可以为你解决这个问题。 (我没有看Canister。我只扫描了瓶子插件的列表。)
    猜你喜欢
    • 1970-01-01
    • 2019-10-05
    • 1970-01-01
    • 2021-10-08
    • 2018-02-22
    • 1970-01-01
    • 2018-11-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多