NameError:全局名称“烧瓶”未定义

时间:2019-08-26 13:01:47

标签: python twitter routes

我对Python Web编程还很陌生。请在运行测试应用程序时请求您的协助以解决我遇到的错误。尝试从Powershell运行命令-pythonrun.py。它给出了标题中给出的错误。 Trying the instructions provided in this link

run.py

from tweet_harvester import app
app.run(port=8080)

Config.py

import os
DEBUG = True 
TWITTER_CONSUMER_KEY = os.environ['TWITTER_CONSUMER_KEY']
TWITTER_CONSUMER_SECRET = os.environ['TWITTER_CONSUMER_SECRET']
TWITTER_ACCESS_TOKEN = os.environ['TWITTER_ACCESS_TOKEN']
TWITTER_ACCESS_TOKEN_SECRET = os.environ['TWITTER_ACCESS_TOKEN_SECRET']

init .py

from flask import Flask, json, request, render_template
import tweepy
app = Flask(__name__)
app.config.from_object('config')
auth = tweepy.OAuthHandler(app.config['TWITTER_CONSUMER_KEY'],app.config['TWITTER_CONSUMER_SECRET'])
auth.set_access_token(app.config['TWITTER_ACCESS_TOKEN'],app.config['TWITTER_ACCESS_TOKEN_SECRET'])
tweepy_api = tweepy.API(auth)

def get_tweets(username):
tweets = tweepy_api.user_timeline(screen_name=username) 
return [{'tweet': t.text,'created_at': t.created_at,'username': username, 'headshot_url': t.user.profile_image_url} 
for t in tweets]

@app.route('/tweet-harvester/<string:username>')
def tweets(username):
return flask.render_template('tweets.html', tweets=get_tweets(username))

tweets.html-仅粘贴标头和正文部分的相关部分

<title>Tweet Harvester</title>
<body>
<div class="container">
    <h1 class="p-3">Tweet Harvester</h1>
    {% for tweet in tweets %}
    <div class="list-group">
        <a href="#" class="list-group-item list-group-item-action flex-column align-items-start">
          <div class="d-flex w-100 justify-content-between">
            <img src="{{tweet.headshot_url}}" class="w-12 p-1 float-left image-thumbnail">
            <h5 class="ml-10 w-75 mb-1">{{ tweet.tweet }}</h5>
            <small>{{ tweet.created_at }}</small>
          </div>
        </a>
    </div>
{% endfor %}

2 个答案:

答案 0 :(得分:1)

您尚未从flask导入render_template。您需要先导入它。

from flask import render_template

答案 1 :(得分:-1)

我猜因为您使用的render_template不正确。 应该是flask.render_template()

以更好的方式使用它(或更好),您只需将其从烧瓶中导入为

from flask import render_template