最后使用的会话类型为e的页面列表

2024-04-25 17:00:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图列出一个用户最近访问过的页面列表,但是我一直得到TypeError: store_visited_urls() takes no arguments (1 given)。 我不知道这是怎么回事。你知道吗

Python/Flask代码:

app.secret_key = '/r/xd8}q/xde/x13/xe5F0/xe5/x8b/x96A64/xf2/xf8MK/xb1/xfdA7x8c'

def recentsites():
    session['urls'] = []

@app.after_request
def store_visited_urls():
    session['urls'].append(request.url)
    if(len[session['urls']]) > 3:
        session['urls'].pop(0)
    session.modified = True

@app.route('/')
def index():
    data = []
    if 'urls' in session:
        data = session['urls']
    return render_template('index.html', data=data)

Tags: store用户app列表dataindexifrequest
1条回答
网友
1楼 · 发布于 2024-04-25 17:00:01

我认为作为一个函数,它自动地包含self作为一个参数。创建类时,此参数将作为调用的一部分包含。你知道吗

创建定义def store_visited_urls(self):,但继续不带参数地调用它。你知道吗

从外观上看session必须在类中定义。因此,您将引用self.session,以便在实例化类时获取它。你知道吗

请参阅What is the purpose of self?以获取解释。你知道吗

我看到您使用的是装饰器,请参见Decorators I: Introduction to Python DecoratorsA guide to Python's function decoratorsPython DecoratorsPrimer on Python Decorators

相关问题 更多 >