2017-05-25 31 views
0

我有一個叫generate_csrf_token的函數,它位於包含以下代碼的包commons.formsecurity中。在請求上下文之外設置Jinja環境全局變量

import random 
import string 
from flask import session 


def generate_csrf_token(): 
    if '_csrf_token' not in session: 
     state = ''.join(random.choice(string.ascii_uppercase + string.digits) 
         for x in xrange(32)) 
     session['_csrf_token'] = state 
    return session['_csrf_token'] 

我在我的create_app函數中調用了這個函數。當我跑我的瓶應用

from flask import Flask 
from routes_manager import configure_blueprints 
from error_handling import configure_error_handling 
from flask import session 
from flask.ext.session import Session 
from commons.formsecurity import generate_csrf_token 

def create_app(): 
    """Create the Flask App""" 
    app = Flask(__name__) 
    app.secret_key = 'lalalalalala' 
    app.jinja_env.globals['csrf_token'] = generate_csrf_token() 
    configure_blueprints(app) 
    configure_error_handling(app) 
    return app 

create_app從main.py

from app import create_app 

app = create_app() 

"""Run the clients""" 
if __name__ == '__main__': 

    app.run(debug=True) 

調用。我收到以下錯誤。

ERROR 2017-05-25 12:12:50,720 wsgi.py:263] 
Traceback (most recent call last): 
    File "C:\Program Files (x86)\Google\google_appengine\google\appengine\runtime\wsgi.py", line 240, in Handle 
    handler = _config_handle.add_wsgi_middleware(self._LoadHandler()) 
    File "C:\Program Files (x86)\Google\google_appengine\google\appengine\runtime\wsgi.py", line 299, in _LoadHandler 
    handler, path, err = LoadObject(self._handler) 
    File "C:\Program Files (x86)\Google\google_appengine\google\appengine\runtime\wsgi.py", line 85, in LoadObject 
    obj = __import__(path[0]) 
    File "C:\Code\App-Engine\cfc-melbourne-website\main.py", line 3, in <module> 
    app = create_app() 
    File "C:\Code\App-Engine\cfc-melbourne-website\app\__init__.py", line 12, in create_app 
    app.jinja_env.globals['csrf_token'] = generate_csrf_token() 
    File "C:\Code\App-Engine\cfc-melbourne-website\app\commons\formsecurity.py", line 7, in generate_csrf_token 
    if '_csrf_token' not in session: 
    File "lib\werkzeug\local.py", line 379, in <lambda> 
    __contains__ = lambda x, i: i in x._get_current_object() 
    File "lib\werkzeug\local.py", line 306, in _get_current_object 
    return self.__local() 
    File "lib\flask\globals.py", line 37, in _lookup_req_object 
    raise RuntimeError(_request_ctx_err_msg) 
RuntimeError: Working outside of request context. 

This typically means that you attempted to use functionality that needed 
an active HTTP request. Consult the documentation on testing for 
information about how to avoid this problem. 

哪裏是調用下面

app.jinja_env.globals['csrf_token'] = generate_csrf_token() 

回答

0

如果你不叫generate_csrf_token()功能,而是存儲參考功能最好的地方,你可以把它在你的神社模板(這將在請求可用的上下文中)。

所以更換

app.jinja_env.globals['csrf_token'] = generate_csrf_token() 

隨着

app.jinja_env.globals['csrf_token'] = generate_csrf_token 

而在你的模板,使用:

<input name=_csrf_token type=hidden value="{{ csrf_token() }}"> 
相關問題