2 回答
TA贡献1796条经验 获得超7个赞
最好像这样重新组织事物,在没有输入位置时不查询事物,如果未设置位置,则不尝试访问:trends
@app.route("/")
def index():
location = request.args.get("location")
trends = None
if location: # Only query if there is a location
loc_id = client.fetch_woeid(str(location))
trends = api.trends_place(int(loc_id))
# the above will throw an error if the place is invalid, that's fine
return render_template("index.html", trends=trends, location=location)
和
<div class="column">
<form method="GET">
<div class="form-group">
<p>See whats trending in your area.</p>
<label for="">Enter location name</label>
<input type="text" name="location" value="{{ location|default("") }}" class="form-control" id="exampleInput" placeholder="example london " required>
<input type="submit" value="Search" class="btn btn-primary">
</div>
</form>
</div>
<div class="column">
{% if trends %}
<h4>Top Trending</h4>
<ul class="list-group">
{% for trend in trends[0]["trends"]%}
<a class="list-group-item list-group-item-action" href="{{trend.url}}">{{trend.name}}</a>
{% endfor %}
</ul>
{% endif %}
</div>
TA贡献1835条经验 获得超7个赞
if 是升高的,则在渲染此行时留下未定义状态。tweepy.error.TweepErrorreturn render_template('index.html')trends{% for trend in trends[0]["trends"]%}
你应该改变
return render_template('index.html')
自
return render_template('index.html', trends=None)
然后检查是否在模板中传递:trends
<div class="column">
<h4>Top Trending</h4>
<ul class="list-group">
{% if trends is not None %}
{% for trend in trends[0]["trends"]%}
<a class="list-group-item list-group-item-action" href="{{trend.url}}">{{trend.name}}</a>
{% endfor %}
{% endif %}
</ul>
</div>
添加回答
举报