Flask url_for 不使用参数
Flask url_for not working with parameters
我有一个需要 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)
Html 使用 url_for 的代码
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 %}
您在路由装饰器中缺少参数 top
,因为 news(top)
视图已经有一个 top
参数
@app.route('/news/<top>') # -- HERE --
def news(top):
[..]
return ..
我有一个需要 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)
Html 使用 url_for 的代码
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 %}
您在路由装饰器中缺少参数 top
,因为 news(top)
视图已经有一个 top
参数
@app.route('/news/<top>') # -- HERE --
def news(top):
[..]
return ..