用flask获取客户端计算机名,如何获取登录用户的名称(Flask,SQLAlchemy)

我想把作者的名字,从日志在会话中博客。

到目前为止,我可以记录用户,但当他已经被记录,我无法找到方式与他的名字在网站上工作。在

所以我尝试创建一些方法,在他登录到内存后存储用户名,然后flask将使用这个用户名来发布博客、评论或编辑个人资料。谢谢你Base = declarative_base()

class User(Base):

__tablename__ = "users"

id = Column(Integer, primary_key=True)

username = Column(String(64))

password = Column(String(120))

email = Column(String(64))

def __init__(self, username, password, email):

self.username = username

self.password = password

self.email = email

Base.metadata.create_all(engine)

Base2 = declarative_base()

class Blogpost(Base2):

__tablename__ = 'blogpost'

id = Column(Integer, primary_key=True)

title = Column(String(50))

subtitle = Column(String(50))

author = Column(String(20))

date_posted = Column(DateTime)

content = Column(Text)

def __init__(self, title, subtitle, author, date_posted, content):

self.title = title

self.subtitle = subtitle

self.author = author

self.date_posted = date_posted

self.content = content

@app.route('/login', methods=['POST'])

def login():

POST_USERNAME = str(request.form['username'])

POST_PASSWORD = str(request.form['password'])

def check_password(hashed_password, user_password):

password, salt = hashed_password.split(':')

return password == hashlib.sha256(salt.encode() + user_password.encode()).hexdigest()

Session = sessionmaker(bind=engine)

s = Session()

user = s.query(User).filter_by(username=POST_USERNAME).first()

if check_password(user.password, POST_PASSWORD) == True:

session['logged_in'] = True

user_name = POST_USERNAME

else:

flash('wrong password!')

return index()

@app.route('/add')

def add():

return render_template('add.html')

@app.route('/addpost', methods=['POST'])

def addpost():

title = request.form['title']

subtitle = request.form['subtitle']

content = request.form['content']

Session = sessionmaker(bind=engine)

session = Session()

post = Blogpost(title=title, subtitle=subtitle, author=user_name, content=content, date_posted=datetime.now())

session.add(post)

session.commit()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值