【问题标题】:Flask url_for not working with parameters [duplicate]烧瓶url_for不使用参数[重复]
【发布时间】:2023-03-31 14:05:02
【问题描述】:

我有一个需要 1 个参数的函数,但我的 url_for 似乎由于某种原因无法正常工作

python 代码

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

使用 url_for 的 HTML 代码

home.html

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

news.html

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

【问题讨论】:

    标签: python html flask url-for


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2019-03-01
      • 2023-03-09
      • 2023-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多