有没有办法删除此代码中的未定义错误?

我试图显示从twitter到我的网页的最热门主题的结果,但是当我运行应用程序时,它会返回jinja2.exceptions.UndefinedError:“趋势”未定义。我怀疑我没有使用尝试/除了它应该如何。


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


MYYA
浏览 91回答 2
2回答

芜湖不芜

最好像这样重新组织事物,在没有输入位置时不查询事物,如果未设置位置,则不尝试访问:trends@app.route("/")def index():&nbsp; &nbsp; location = request.args.get("location")&nbsp; &nbsp; trends = None&nbsp; &nbsp; if location:&nbsp; # Only query if there is a location&nbsp; &nbsp; &nbsp; &nbsp; loc_id = client.fetch_woeid(str(location))&nbsp; &nbsp; &nbsp; &nbsp; trends = api.trends_place(int(loc_id))&nbsp; &nbsp; &nbsp; &nbsp; # the above will throw an error if the place is invalid, that's fine&nbsp; &nbsp; return render_template("index.html", trends=trends, location=location)和<div class="column">&nbsp; &nbsp; <form method="GET">&nbsp; &nbsp; &nbsp; &nbsp; <div class="form-group">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p>See whats trending in your area.</p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label for="">Enter location name</label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="text" name="location" value="{{ location|default("") }}" class="form-control" id="exampleInput" placeholder="example london " required>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="submit" value="Search" class="btn btn-primary">&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </form></div><div class="column">&nbsp; &nbsp; {% if trends %}&nbsp; &nbsp; <h4>Top Trending</h4>&nbsp; &nbsp; <ul class="list-group">&nbsp; &nbsp; &nbsp; &nbsp; {% for trend in trends[0]["trends"]%}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a class="list-group-item list-group-item-action" href="{{trend.url}}">{{trend.name}}</a>&nbsp; &nbsp; &nbsp; &nbsp; {% endfor %}&nbsp; &nbsp; &nbsp; </ul>&nbsp; &nbsp; {% endif %}</div>

qq_花开花谢_0

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">&nbsp; &nbsp; <h4>Top Trending</h4>&nbsp; &nbsp; <ul class="list-group">&nbsp; &nbsp; &nbsp; &nbsp; {% if trends is not None %}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {% for trend in trends[0]["trends"]%}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a class="list-group-item list-group-item-action" href="{{trend.url}}">{{trend.name}}</a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {% endfor %}&nbsp; &nbsp; &nbsp; &nbsp; {% endif %}&nbsp; &nbsp; </ul></div>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python