需要修改:
在路由映射中添加了字典,内部传递了两个参数。
import tornado.web
from views import index
import config
class Application(tornado.web.Application):
def __init__(self):
handlers = [
(r'/',index.IndexHandler,{"userid":"1","username":"gtd"})
]
# 调用父类Application,传递handlers,传递配置
super(Application,self).__init__(handlers,**config.settings)
添加了def initialize(self,userid,username): 函数并接收两个参数。
from tornado.web import RequestHandler
# 业务处理类
class IndexHandler(RequestHandler):
# 传参,该方法会在HTTP方法前调用
def initialize(self,userid,username):
self.userid = userid
self.username = username
# 处理get请求
def get(self,*args,**kwargs):
print(self.username,self.userid)
self.write("hello world")
如下所示
(r’/send_parameter/(\w+)/(\w+)',index.SendParameterUrlHandler)
完整代码如下:
import tornado.web
from views import index
import config
class Application(tornado.web.Application):
def __init__(self):
handlers = [
(r'/',index.IndexHandler,{"userid":"1","username":"gtd"}),
# 将参数融入地址
(r'/send_parameter/(\w+)/(\w+)',index.SendParameterUrlHandler)
]
# 调用父类Application,传递handlers,传递配置
super(Application,self).__init__(handlers,**config.settings)
# 从url中获取参数
class SendParameterUrlHandler(RequestHandler):
def get(self,name,id,*args,**kwargs):
print(f"SendParameterUrlHandler : {name}--{id}")
self.write("厉害")
完整代码如下:
from tornado.web import RequestHandler
# 业务处理类
class IndexHandler(RequestHandler):
# 传参,该方法会在HTTP方法前调用
def initialize(self,userid,username):
self.userid = userid
self.username = username
# 处理get请求
def get(self,*args,**kwargs):
print("IndexHandler : ",self.username,self.userid)
self.write("hello world")
# 从url中获取参数
class SendParameterUrlHandler(RequestHandler):
def get(self,name,id,*args,**kwargs):
print(f"SendParameterUrlHandler : {name}--{id}")
self.write("厉害")
(r’/send_parameter_get’, index.SendParameterGetHandler)
完整代码:
import tornado.web
from views import index
import config
class Application(tornado.web.Application):
def __init__(self):
handlers = [
(r'/',index.IndexHandler,{"userid":"1","username":"gtd"}),
# 将参数融入地址
(r'/send_parameter/(\w+)/(\w+)',index.SendParameterUrlHandler),
# get参数传递
(r'/send_parameter_get', index.SendParameterGetHandler)
]
# 调用父类Application,传递handlers,传递配置
super(Application,self).__init__(handlers,**config.settings)
# get参数获取
class SendParameterGetHandler(RequestHandler):
def get(self,*args,**kwargs):
name = self.get_query_argument(name="name",default="NO NAME",strip=False)
id = self.get_query_argument(name="id",default="NO ID",strip=True)
print(f"SendParameterGetHandler : {name}--{id}")
self.write("get参数获取")
完整代码:
from tornado.web import RequestHandler
# 业务处理类
class IndexHandler(RequestHandler):
# 传参,该方法会在HTTP方法前调用
def initialize(self,userid,username):
self.userid = userid
self.username = username
# 处理get请求
def get(self,*args,**kwargs):
print("IndexHandler : ",self.username,self.userid)
self.write("hello world")
# 从url中获取参数
class SendParameterUrlHandler(RequestHandler):
def get(self,name,id,*args,**kwargs):
print(f"SendParameterUrlHandler : {name}--{id}")
self.write("url中参数获取")
# get参数获取
class SendParameterGetHandler(RequestHandler):
def get(self,*args,**kwargs):
name = self.get_query_argument(name="name",default="NO NAME",strip=False)
id = self.get_query_argument(name="id",default="NO ID",strip=True)
print(f"SendParameterGetHandler : {name}--{id}")
self.write("get参数获取")
(r’/send_parameter_post’, index.SendParameterPostHandler)
简单编写前端
Title
POST请求页面
class SendParameterPostHandler(RequestHandler):
# get请求
def get(self,*args,**kwargs):
# 返回页面
self.render('index.html')
def post(self,*args,**kwargs):
name = self.get_body_argument("name",default="NO NAME")
id = self.get_body_argument("id",default="NO NAME")
print(f"SendParameterPostHandler : {name}--{id}")
self.write("POST请求成功")