为什么在Flask代码中出现TypeError?

时间:2018-12-01 14:57:48

标签: python flask

我正在执行以下Flask代码,然后收到以下错误。

TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement.

我不太清楚代码中的含义和错误。

代码:

from flask import Flask, render_template
import urllib.request
import json
import time

app = Flask(__name__ ,template_folder='template')
namep = "PewDiePie"
namet = "TSeries"
key = "MY_API_KEY"

@app.route("/")
def function_main():
 datat = urllib.request.urlopen("https://www.googleapis.com/youtube/v3/channels?part=statistics&forUsername="+namep+"&key="+key).read()
 datap = urllib.request.urlopen("https://www.googleapis.com/youtube/v3/channels?part=statistics&forUsername="+namet+"&key="+key).read()
 subt = json.loads(datat)["items"][0]["statistics"]["subscriberCount"]
 subsp = json.loads(datap)["items"][0]["statistics"]["subscriberCount"]
 def main():
   return render_template('template\index.html', subsp = subsp, subt = subt)#Sends the integers to index.html to get printed in the Flask server

if __name__ == "__main__":
 app.run(debug=True, host="0.0.0.0", port=80)

任何帮助将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:0)

from flask import Flask, render_template
import urllib.request
import json
import time

app = Flask(__name__ ,template_folder='template')
namep = "PewDiePie"
namet = "TSeries"
key = "MY_API_KEY"

@app.route("/")
def function_main():
 datat = urllib.request.urlopen("https://www.googleapis.com/youtube/v3/channels?part=statistics&forUsername="+namep+"&key="+key).read()
 datap = urllib.request.urlopen("https://www.googleapis.com/youtube/v3/channels?part=statistics&forUsername="+namet+"&key="+key).read()
 subt = json.loads(datat)["items"][0]["statistics"]["subscriberCount"]
 subsp = json.loads(datap)["items"][0]["statistics"]["subscriberCount"]
 return render_template('template\index.html', subsp = subsp, subt = subt)#Sends the integers to index.html to get printed in the Flask server

if __name__ == "__main__":
 app.run(debug=True, host="0.0.0.0", port=80)