Section 5: Debugging

Debug 之心碎历程

1. 只在终端运行,浏览器页面出不来(while 函数问题)

一开始 run web.py 时,直接在终端显示数据而打不开浏览器。 请教faketooth: 原查询天气预报数据的api.py 里的 while 函数在调用时变成了全局变量。

while True:
    cityname = input('You can enter city name in Chinese or English:')
    if cityname == 'help':

faketooth :

  1. 这个 while 循环看着正常,但实际上在import的时候就是个雷。
  2. import 的时候,api.py里的代码会被编译执行。def 方式定义的function代码不会被执行,只是检查语法 但是,while循环那里,作为全局的代码,可就是直接被执行了。
  3. 所以,因为api.py里while循环的问题,在import的时候先直接陷入到api.py的while循环逻辑中,也就是你的页面出不来的原因。

解决办法:把 while 循环装在一个函数里。

def main():
    while True:

2. 忘记在定义处理数据函数时,在绘制结果集的地方 render_template 部分把数据传入(没有加上 data = data)

faketooth : 我还是基于这一份代码来评论吧。 index.html里面,设置整个form的方法为post,所以你下面的elif request.method == 'GET'完全没有用 index.html里的代码,需要用data,history,和help来区分是打印哪些内容。但是,所有的render_template中都没有传递相应的参数呐。告诉你一个技巧,在每个分支下面加<p>something to tag this branch</>就能识别到代码走的哪个分支

解决办法

1.index.html 里加了另一个 GET 的属性,也就是 <form action="/" method="GET" >,算是暂时解决了问题。

但这又挖出另一个坑:查询天气预报数据时同时调用了 'POST' and 'GET,这个作为第三个问题在后面说明。

2.没有传递参数问题主要在于: index.html 里:

<input type="submit" name="history" value="HISTORY">
<input type="submit" name="help" value="HELP">

跟 web.py 里

if request.args.get("button") == 'history':
            return render_template('index.html',
            history = listsearch[0])
        if request.args.get("button") == 'help':
            return render_template('index.html',
            help=' ')

显然完全没有建立关联啊~

这也是faketooth提醒我的:"嗯,实际上你还会遇到一个bug,就是这一段,琢磨一下:"

 data = fetchAPI(cityname)
        m = data
        listsearch.append(m[0])
        return render_template('index.html',
            cityname = listsearch[0],
            weather = listsearch[1],
            temp = listsearch[2],
            wind = listsearch[3])

这个data = data 没有加入 render_tmplate 不能说是坑,就是语法不熟埋的雷... 应该是:

def index():
        if request.method =='POST':
            cityname = request.form['cityname']
            data = fetchAPI(cityname)
            m = data
            listsearch.append(m[0])
            return render_template('index.html',
                data = data,          #这里一开始没有写
                cityname = data[0],
                weather = data[1],
                temp = data[2],
                wind = data[3])

3. 查询天气预报数据时同时调用了 'POST' and 'GET**

我在 index.html 同时写了

  <form action="/" method="POST" >
    <label> Cityname <label>
      <input type="text" name="cityname">
  </form>

以及:

 <form action="/" method="GET" >
      <input type="submit" name="query" value="SEARCH">
      <input type="submit" name="history" value="HISTORY">
      <input type="submit" name="help" value="HELP">
  </form>

所以按下 SEARCH button 时终端可以显示数据,但浏览器出不来。 faketooth:" html 里不可能走两个分支。"、 然后我就开始想啊想,后来才明白是怎么回事(好吧其实还没有完全明白)。尝试把

<input type="submit" name="query" value="SEARCH">

这一行删掉。 这样输入 cityname 后直接回车键才运行了,也就是说只走 “POST“这个路径。但history and help 还没有正常显示(这个坑后来填上了)。并且失去了 SEARCH 这个 button (这个坑还没有填)。

其他同学也有类似情况,参照这份comment

faketooth:“route是有顺序的,所有请求都被第一个拦截了。” simpleowen:“因为查询天气用得是post方法,第一个路由没有指定方法,所以默认使用GET方法。获取get 方法的数据好像要用request.args.get('button')。”

  elif request.method == 'GET':
    if request.args.get('button') == 'History':
      return render_template('main.html',help=' ')
    elif request.args.get('button') == 'Help':
      return render_template('main.html', history=listH)
    else:
      return render_template('main.html')

4.关于 web browser 查询的 History(web.py)是否与原 API 查询的历史记录是否要一致。

关于ch3 api.py 里历史查询记录是否要和ch4的一致问题 这个问题纯粹是我自己概念不清挖出来的大坑。 最初搞不清查询listhistory是否应该为全局变量,也就是说 ch3那个作业获取api.py里的history和ch4执行的对数据进行处理的web.py里的是否为同一个,是否应该把ch3里的listhistory import进来到ch4。

微信答疑大妈说不必,且有三种方式解决。好吧我只想到两个。

5.其他的:

至于把 html 里 method 写成 methods,以及有时多了个括号,少了个逗号等等这种小坑我真是没脸提了...

6.I WILL BE BACK...

May the "SEARCH" button come back to me...

search

BUT, HOW ?

Still have no idea ... how

results matching ""

    No results matching ""