gpt4 book ai didi

python - 在 Flask 应用程序中设置日期时间

转载 作者:太空宇宙 更新时间:2023-11-03 15:49:04 24 4
gpt4 key购买 nike

我正在开发一个 Flask 应用程序,我想知道是否有人可以帮助我弄清楚为什么我在新用户注册帐户时尝试设置日期时间时收到错误。我只是希望它保存用户注册的确切时间。以下是我创建的用于将用户添加并提交到数据库的路由。

from flask import Flask, render_template, request, redirect, url_for, flash
from flask import escape, session
from datetime import datetime
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from database import Users, Base

app.route('/registered', methods=['Get', 'POST'])
def Register():
if request.method == 'POST':
password = request.form['password']
verify = request.form['verify']
if password == verify:
newUser = Users(Username = request.form['username'],
Password = request.form['password'],
Email = request.form['email'],
DateTime = datetime.now())
session.add(newUser)
session.commit()
return redirect('HomePage')
else:
flash('Passwords do not match')
return render_template('Register.html')

这是我当前设置的数据库结构:

class Users(Base):
__tablename__ = 'users'

Id = Column(Integer, primary_key = True)
Username = Column(String(22), unique = True, nullable = False)
Password = Column(String(22), nullable = False)
Email = Column(String(55), unique = True, nullable = False)
Registered = Column(DateTime, nullable = False)

我不确定 html 文件是否有任何设置日期时间属性的内容。

如果有人能帮忙的话。我们将不胜感激。

最佳答案

在请求处理程序中,您将关键字参数 DateTime 传递给 Users 构造函数:

app.route('/registered', methods=['Get', 'POST'])
def Register():
if request.method == 'POST':
password = request.form['password']
verify = request.form['verify']
if password == verify:
newUser = Users(Username = request.form['username'],
Password = request.form['password'],
Email = request.form['email'],
DateTime = datetime.now())

但是Users模型没有DateTime列,正确的名称是Registered:

class Users(Base):
__tablename__ = 'users'

Id = Column(Integer, primary_key = True)
Username = Column(String(22), unique = True, nullable = False)
Password = Column(String(22), nullable = False)
Email = Column(String(55), unique = True, nullable = False)
Registered = Column(DateTime, nullable = False)

关于python - 在 Flask 应用程序中设置日期时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41478753/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com