Я пытаюсь отобразить результаты самых популярных тем из твиттера на моей веб-странице, но когда я запускаю приложение, оно возвращает jinja2.exceptions.UndefinedError: «тренды» не определены. Я подозреваю, что я не использую try /, за исключением того, как он должен.
@app.route('/')
def index():
try:
location = request.args.get('location')
loc_id = client.fetch_woeid(str(location))
trends = api.trends_place(int(loc_id))
return render_template('index.html',trends=trends)
except tweepy.error.TweepError:
return render_template('index.html')
Я также думаю, что есть проблема в моем коде шаблона.
<div class="column">
<form method="GET" action="{{url_for('index')}}">
<div class="form-group">
<p>See whats tending in your area.</p>
<label for="">Enter location name</label>
<input type="text" name="location" class="form-control" id="exampleInput" placeholder="example london " required>
<input type="submit" value="Search" class="btn btn-primary">
</div>
</form>
</div>
<div class="column">
<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>
</div>