You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flask+SQLAlchemy+Session实现仅姓名登录注册问题求助

问题:仅基于姓名的登录注册功能实现问题(存入SQLite数据库及Session)

我正在开发一个供家人向我求助的网页,已掌握基于Twilio的消息发送核心功能,但无法实现仅基于姓名的登录注册功能,包括将用户姓名存入SQLite数据库及Session中。

Python代码

from twilio.rest import Client
from flask import Flask,render_template,request, redirect, url_for, session, flash
from datetime import timedelta
from flask_sqlalchemy import SQLAlchemy


app = Flask(__name__)

account_sid = 'ACCOUNT SID'
auth_token = 'AUTH-TOKEN'
my_number='PHONE NUMBER'
twillio_number = 'TWILIO NUMBER'
client = Client(account_sid, auth_token)
app.secret_key = "secret key"
app.permanent_session_lifetime = timedelta(minutes=30)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///users.sqlite3'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
class Users(db.Model):
    _id = db.Column("id", db.Integer, primary_key=True)
    name = db.Column(db.String(100))
        
    def __init__(self, name):
         self.name = name

@app.route("/")
def index():
    return redirect(url_for("login"))



@app.route("/login", methods=['POST', 'GET'])
def login():
    if request.method == "POST":
        username = request.form.get('username')   
        usr = Users(name=username)
        session['user'] = usr
        return redirect(url_for('msg'))
    return render_template('indextwo.html')
    

@app.route("/msg", methods=['POST', 'GET'])
def home():
    if 'user' in session:
        user = session['user']
        message = ()
        if request.method == "POST":
            msgval = request.form.get('msg')
            if msgval:
                message = client.messages.create(
                body = msgval,
                from_ = twillio_number,
                to = my_number
                )
            else:
                msgvall = request.form.get('exa')
                if msgvall:
                    message = client.messages.create(
                    body = msgvall,
                    from_ = twillio_number,
                    to = my_number
                    )

    return render_template('index.html', username=user, content=message)
if __name__ == "__main__":
    app.run(debug=True, host='0.0.0.0', port=80)

登录页面HTML(indextwo.html)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Log in</title>
</head>
<body>
    <form action="#" methods="POST">
        <input type="text" name="username">
        <input type="submit" value="LOG IN">
    </form>
</body>
</html>

主页HTML(index.html)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>HelperMessage📧</title>
    <style>
hr{
    border-color:rgb(151, 144, 144);
    width:75%;

}
form{
    text-align: center;
    margin-bottom: 20px;
}
.askforhelp{
margin-top: 20px;
}
.f2{
    margin-top: 20px;
}
    </style>
</head>
<body> 
<div class="askforhelp"> 
    <form action="#" method="post">
        <select name="exa">
            <option>Select one if you don't want to type</option>
            <option>Need help with tech</option>
            <option>Need to watch Zara</option>
            <option>Need to lift something heavy</option>
            <option>Need me to go buy something</option>
        </select>
        <input type="submit" value="Send"/>
    </form>
    <hr>
    <form action="#" method="post" class="f2">
        <input type="text" name="msg" />
        <input type="submit" value="Send"/>
    </form>
</div>
</body>
</html>

我知道自己的代码不够完善,正通过项目学习提升编程技能,希望得到帮助。

内容的提问来源于stack exchange,提问作者user21841027

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 18:20:40