[英]View function did not return a response in flask
I have a code below which setups cookie and then adds this cookie to the response with set_cookies
function. 我在下面有一个代码设置cookie,然后使用
set_cookies
函数将此cookie添加到响应中。 However, even though I return a response I received the following error: 但是,即使我返回响应,我收到以下错误:
ValueError: View function did not return a response
My code is simply this: 我的代码就是这样:
def login():
if request.method == "POST":
timestamp = str(int(time.time()))
cookie = timestamp+'user'
cookie = base64.b64encode(cookie.encode('utf-8')).decode('utf-8')
resp = make_response()
resp = resp.set_cookie("LoginCookie",cookie)
return resp
response.set_cookie is an in-place operation. response.set_cookie是一个就地操作。 This means that if you set something to it's return value, it will be None, which is the default return value (functions with no return return None).
这意味着如果你设置了它的返回值,它将是None,这是默认的返回值(没有返回的函数返回None)。 What should be used instead is:
应该使用的是:
resp.set_cookie("LoginCookie",cookie)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.