今天遇见一个很奇怪的问题,使用tornado搭建服务器,第一次我是这样写的
self.render('index.html',name = "name")
在页面{{name}}可以正常取值,随后又加了几个参数就开始报错
import os
import tornado.web
import tornado.ioloop
import tornado.options
tornado.options.parse_command_line()
class IndexHandler(tornado.web.RequestHandler):
def get(self,*args, **kwargs):
self.write('<h1>hello tornado</h1>')
self.render('index.html',name='name',clist=['2','3','4'],mdict={'k':'v'})
if __name__ == "__main__":
settings = {
"template_path": os.path.join(os.path.dirname(__file__), "templates"),
"debug": True
}
app = tornado.web.Application(
[
(r"^/index$", IndexHandler),
],**settings
)
httpServer = tornado.httpserver.HTTPServer(app)
httpServer.bind(8000)
httpServer.start(1)
tornado.ioloop.IOLoop.current().start()
[E 220320 14:17:09 web:1670] Uncaught exception GET /index (127.0.0.1)
HTTPServerRequest(protocol='http', host='127.0.0.1:8000', method='GET', uri='/index', version='HTTP/1.1', remote_ip='127.0.0.1')
Traceback (most recent call last):
File "D:\py_virtual_env\fuyiTornado\lib\site-packages\tornado\web.py", line 1590, in _execute
result = method(*self.path_args, **self.path_kwargs)
File "D:/project/PPY/pycharmProjects/tornado_day/test1/quantao/server.py", line 18, in get
self.render('index.html',uname='zhangsan',clist=['2','3','4'],mdict={'k':'v'})
File "D:\py_virtual_env\fuyiTornado\lib\site-packages\tornado\web.py", line 766, in render
html = self.render_string(template_name, **kwargs)
File "D:\py_virtual_env\fuyiTornado\lib\site-packages\tornado\web.py", line 907, in render_string
return t.generate(**namespace)
File "D:\py_virtual_env\fuyiTornado\lib\site-packages\tornado\template.py", line 346, in generate
return execute()
File "index_html.generated.py", line 5, in _tt_execute
_tt_tmp = name
NameError: name 'name' is not defined
[E 220320 14:17:09 web:2162] 500 GET /index (127.0.0.1) 9.00ms
[I 220320 14:17:53 autoreload:199] D:/project/PPY/pycharmProjects/tornado_day/test1/quantao/server.py modified; restarting server
折腾了半天,终于发现问题了 原来是html页面这里的注释没有用,需要吧这些取值删掉才行。
|