渲染模板时 Flask 无法初始化我的类

问题描述

我使用烧瓶:

from flask import Flask,render_template,session

我想在一个类中封装一些会话操作方法是这样的:

class Session:
    def __init__(self):
        self.ip = request.remote_addr
        self.name = request.remote_user
        session['ip'] = self.ip
        session['name'] = self.name

为了通过下面的函数在模板中呈现这两个属性

@app.route('/',methods=['GET'],strict_slashes=False)
def indexpage():
    return render_template('index.html',s=Session())

但是,不同的设备获得相同的输出,这意味着 Session.ipSession.name 来自不同的设备是相同的。我认为类 Session 未正确初始化。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)