微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Flask url_for 不使用参数

如何解决Flask url_for 不使用参数

我有一个接受 1 个参数的函数,但看起来我的 @app.route('/news') def news(top): client = gnewsclient.NewsClient(language='english',location='india',topic=f'{top}',max_results=50) news_list = client.get_news() topics = client.topics return render_template('news.html',data=news_list,data2=topics) 由于某种原因无法正常工作

python 代码

<a href="{{url_for('news',top='world')}}">News</a>

使用 url_for 的 Html 代码

home.html

    {% for item in data2 %}
        <a id="link" href="{{url_for('news',top= item)}}">
            <div id="topic" style="color: white">{{item}}</div>
        </a>
    {% endfor %}

news.html

{{1}}

解决方法

您在路由装饰器中缺少参数 top,因为 news(top) 视图已经有一个 top 参数

@app.route('/news/<top>')  # -- HERE --
def news(top):
    [..]
    return ..

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。