怎样用python的falsk库写几行代码,让程序的"def index():"中渲染上″index.html",再到"def login():"中显示“已注册”并将index.html的post表単中三个值:"username"、"phoneid"、"password"写入文件?
python代码(有错,希望指出)
#python mian.py
from flask import Falsk,render_template,requst
app = Falsk(__name__)
f = open("注册信息.txt","a")
@app.route("/")
def index():
return render_template("index.html")
@app.route("/login",methods=["POST"])
def login():
name = request.values.get("username")
id = request.values.get("phoneid")
pd = request.values.get("password")
fn = "用户名:" + name + "\n"
fn += "手机号:" + id + "\n"
fn += "密码:" + pd + "\n\n"
f.write(fn)
f.close()
app.run()
html代码
#html index.html
html>
<html lang="zh">
<head>
<meta charset="UTF-8">
head>
<body>
<h1>注册h1>
<h2>信息记录h2>
<form method ="POST" action ="/login">
请输入你的用户名(Username)<input type ="text" name ="username" placeholder ="input your user"><br />
请输入你的手机号(Phone)<input type ="text" name ="phoneid" placeholder ="input your phone"><br />
请设置密码(Password)<input type= "password" name ="password" placeholder ="input your password"><br />
<input type ="submit" name ="submit">
form>
body>
html>
.py目录下新建一个templates目录,把你的html文件放到templates里面去。并且在templates下新建一个__init__.py文件。
from flask import Flask, render_template, request
app = Flask(__name__)
f = open('./注册信息.txt', 'a')
@app.route('/')
def index():
return render_template('./index.html')
@app.route('/login', methods=['POST'])
def login():
name = request.values.get('username')
id = request.values.get('phoneid')
pd = request.values.get('password')
fn = '用户名:' + name + '\n'
fn += '手机号:' + id + '\n'
fn += '密码:' + pd + '\n'
f.write(fn)
f.close()
return '登录成功'
if __name__ == '__main__':
app.run()
什么错?
Flask的入门级使用建议参考https://hpg123.blog.csdn.net/article/details/125445422(Flask 的完全使用教程【文件上传下载,cookie,session,template使用】)。