如何在Flask中并行返回jsonify对象和渲染模板

时间:2019-11-28 06:08:25

标签: flask jwt

我想使用以下登录功能。将JWT令牌保存在cookie中之后,我想重定向到用户home。

@app.route("/login", methods=['GET','POST'])
def login():
    signInform = LoginForm()
    examForm = PersonExamForm()
    online_users = mongo.db.users.find({"online": True})
    try:
        render_template('login.html', title = 'Login', form =signInform) db_cm.find_one({'username':signInform.username.data}) 
        if auth != None:
            print(signInform.password.data == auth['password'])
            if signInform.password.data == auth['password']:       
               access_token = create_access_token(identity=auth['username'], expires_delta=datetime.timedelta(minutes=1), user_claims=None, headers=None)
               refresh_token =create_refresh_token(identity=auth['username'], expires_delta=datetime.timedelta(minutes=1), user_claims=None, headers=None)
            # Set the JWT cookies in the response                
               resp = jsonify({'login': True})
               set_access_cookies(resp, access_token)
               set_refresh_cookies(resp, refresh_token)               
               return resp,200, render_template('home.html', posts = sample_posts, online_users=online_users)              

它不起作用,我收到一条错误消息:

  

ValueError:没有足够的值可解包(预期2,得到1)   127.0.0.1 - - [28/Nov/2019 13:37:48] "POST /login HTTP/1.1" 500 - Traceback (most recent call last): File "C:\Users\.virtualenvs\Test_Projects-7jm6kxsT\lib\site-packages\flask\app.py", line 2463, in __call__ return self.wsgi_app(environ, start_response) File "C:\Users\.virtualenvs\Test_Projects-7jm6kxsT\lib\site-packages\flask\app.py", line 2449, in wsgi_app response = self.handle_exception(e) File "C:\Users\.virtualenvs\Test_Projects-7jm6kxsT\lib\site-packages\flask\app.py", line 1866, in handle_exception reraise(exc_type, exc_value, tb) File "C:\Users\.virtualenvs\Test_Projects-7jm6kxsT\lib\site-packages\flask\_compat.py", line 39, in reraise raise value File "C:\Users\.virtualenvs\Test_Projects-7jm6kxsT\lib\site-packages\flask\app.py", line 2446, in wsgi_app response = self.full_dispatch_request() File "C:\Users\.virtualenvs\Test_Projects-7jm6kxsT\lib\site-packages\flask\app.py", line 1952, in full_dispatch_request return self.finalize_request(rv) File "C:\Users\.virtualenvs\Test_Projects-7jm6kxsT\lib\site-packages\flask\app.py", line 1967, in finalize_request response = self.make_response(rv) File "C:\Users\.virtualenvs\Test_Projects-7jm6kxsT\lib\site-packages\flask\app.py", line 2142, in make_response rv.headers.extend(headers) File "C:\Users\.virtualenvs\Test_Projects-7jm6kxsT\lib\site-packages\werkzeug\datastructures.py", line 1091, in extend for key, value in iterable: ValueError: not enough values to unpack (expected 2, got 1) 127.0.0.1 - - [28/Nov/2019 13:37:48] "GET /login?__debugger__=yes&cmd=resource&f=style.css HTTP/1.1" 200 - 127.0.0.1 - - [28/Nov/2019 13:37:48] "GET /login?__debugger__=yes&cmd=resource&f=jquery.js HTTP/1.1" 200 - 127.0.0.1 - - [28/Nov/2019 13:37:48] "GET /login?__debugger__=yes&cmd=resource&f=debugger.js HTTP/1.1" 200 - 127.0.0.1 - - [28/Nov/2019 13:37:48] "GET /login?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1" 200 - 127.0.0.1 - - [28/Nov/2019 13:37:48] "GET /login?__debugger__=yes&cmd=resource&f=ubuntu.ttf HTTP/1.1" 200 - 127.0.0.1 - - [28/Nov/2019 13:37:49] "GET /login?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1" 200 -   我该如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

您可能正在寻找类似这样的东西:

resp = make_response(render_template(*args))
set_access_cookies(resp, access_token)
set_refresh_cookies(resp, refresh_token)               
return resp, 200