小编Pel*_*can的帖子

瓶子:如何在 python 装饰器中设置 cookie?

在运行某些路由之前,需要完成一些操作。例如 :

  • 检查我们是否识别出用户,
  • 检查语言,
  • 检查位置,
  • 在 html 的导航栏(此处在命名标题之后)中设置变量

依此类推,然后根据结果做出决定,最后运行请求的路线。

我发现很难在装饰器中使用 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)

python cookies http bottle python-3.x

7
推荐指数
1
解决办法
359
查看次数

Python:带有 **kwargs 的类定义

尝试实例化以下类时,出现以下错误:

"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)

python oop instance

6
推荐指数
1
解决办法
5663
查看次数

标签 统计

python ×2

bottle ×1

cookies ×1

http ×1

instance ×1

oop ×1

python-3.x ×1