request返回一个类似字典的Querydict,直接通过键查找,如果不存在该键会报错
def login_handler(request):
if request.method == "GET":
request_params = request.GET
else:
request_params = request.POST
print(type(request_params))
msg = "name=%s,pwd=%s"%(request_params["user_nam"],request_params["user_pwd"])
return HttpResponse(msg)

request返回一个类似字典的Querydict,包含的方法类似字典,可用get方法,找不到键名时返回None
def login_handler(request):
if request.method == "GET":
request_params = request.GET
else:
request_params = request.POST
print(type(request_params))
msg = "name=%s,pwd=%s"%(request_params.get("user_name"),request_params.get("user_pwd"))
return HttpResponse(msg)

print(type(request_params)) 这句话不显示在网页端,显示在linux后端

对于name的值相同来说,通过get方法无法获得所有的值(字典后面的键会覆盖前面的键),可以通过getlist方法获得所有的值。



修改为getlist方法:
msg = "name=%s,pwd=%s,hobby:%s"%(request_params.get("user_name"),request_params.get("user_pwd"),request_params.getlist("user_hobby"))

网友评论