順便寫了ajax版的,主要是javascript的操作。
code要寫的稍微多一點,不過這樣可以分離html,而且更靈活。
index.py
from flask import Flask, render_template, request
from flask_sqlalchemy import SQLAlchemy
import os
app = Flask(__name__)
# 將db放在專案的資料夾
db_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "test.db")
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:{}'.format(db_path)
db = SQLAlchemy(app)
class Person(db.Model):
tablename = 'person'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(255))
age = db.Column(db.Integer)
# index.html目前僅列出所有人之id
@app.route('/')
def all_persons():
persons = Person.query.all() # 先取得所有人
# 傳回所有的人
return render_template("index.html&#