在运行某些路由之前,需要完成一些操作。例如 :
依此类推,然后根据结果做出决定,最后运行请求的路线。
我发现很难在装饰器中使用 respose.set_cookie("cookie_name", actual_cookie)。似乎flask 有一个运行良好的“make_response”对象(参见堆栈溢出问题34543157:Python Flask - 使用装饰器设置cookie),但我发现很难用bottle 重现同样的事情。
无论如何,我的尝试不起作用:
#python3
#/decorator_cookie.py
from bottle import request, response, redirect
from other_module import datamodel, db_pointer, secret_value #custom_module
import json
cookie_value = None
surfer_email_exist_in_db = None
header = None
db_pointer = instanciation_of_a_db_connexion_to_tables
surfer = db_pointer.get(request.get_cookie('surfer')) if db_pointer.get(request.get_cookie('surfer')) != None else "empty"
def set_header(func):
def header_manager():
global cookie_value, surfer_email_exist_in_db, header, db_pointer
cookie_value = True #for stack-overflow question convenience
surfer_email_exist_in_db = True #for stack-overflow question …Run Code Online (Sandbox Code Playgroud) 尝试实例化以下类时,出现以下错误:
"TypeError: __init__() takes exactly 2 arguments (3 given)"
Run Code Online (Sandbox Code Playgroud)
你知道会出现什么问题吗?这是类定义:
class db_create_table():
'''
doc here
'''
def __init__(self,TableName, **kwargs ):
self.TableName = TableName
for k,v in kwargs.iteritems():
setattr(self, k, k)
schema = {"id" : { "type":"Integer", "primary":"primary_key=True", "unique":"unique = True"},
"col1" : { "type":"String()", "primary":"primary_key=False", "unique":"unique = True"},
"col2" : { "type":"String()", "primary":"primary_key=False", "unique":"unique = False"},
"col3" : { "type":"String()", "primary":"primary_key=False", "unique":"unique = False"},
"col4" : { "type":"String()", "primary":"primary_key=False", "unique":"unique = False"},
"CreatedOn" : { "type":"DateTime", "primary":"", "unique":"unique = False"}, …Run Code Online (Sandbox Code Playgroud)