【问题标题】:Trying to call a flask function with parameters from html [duplicate]尝试使用来自 html 的参数调用烧瓶函数 [重复]
【发布时间】:2019-11-09 12:31:55
【问题描述】:

所以我调用了一个烧瓶函数,它从我的 html 代码中接受一个参数检查,我收到错误:TypeError: categories_html() missing 1 required positional argument: 'check'。

这是我的 Python 代码:

app.route('/cat_html', methods=['GET'])
def categories_html(check):
    if session.get('teacher_name'):
        return trav0.gen(check)

    else:
        return redirect('/teachers')

然后这里是 html 调用:

<li class="nav-item">
    <a class="navbar-brand" href="{{ url_for('categories_html', check='sta10') }}" target="_blank" style="color:31708f;">Tasks</a>
</li>

所以trav0.gen生成一个html,check就等于一个数字,根据数字生成一个不同的html。

【问题讨论】:

    标签: python html flask


    【解决方案1】:

    您希望cat_html 的网址看起来像什么并不是 100% 清楚。

    你可以做几件事。

    对于像/cat_html/sta10 这样的网址,您可以编写如下方法:

    @app.route('/cat_html/<check>', methods=['GET'])
    def categories_html(check):
        # use check here
        return check
    

    check 将成为 url 路径的一部分。

    要将检查作为带有/cat_html/?check=sta10 之类的url 的查询参数,您不要将它作为参数添加到函数中,而是在函数中get() 它:

    @app.route('/cat_html/', methods=['GET'])
    def categories_html():
        check = request.args.get('check', '')
        # use check here
        return check
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-31
      • 2023-03-22
      • 2015-12-19
      • 1970-01-01
      • 2017-08-15
      • 2018-02-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多