flask靈活的文件組織結構讓不少新手玩家痛苦萬分,網上的相關教程也是參差不齊,本人也是在幾近崩潰的邊緣瘋狂試探
,堅持即時勝利,終於被我摸索出一個較爲適用的簡單文件組織結構. 本文從單文件結構,逐步拆分紅多個部分,至於其中
各個步驟拆分的用意,還請恕在下才疏學淺,難以用文字形容,各位看官請自行體會.html
目錄結構python
app.py
mysql
from flask import Flask, render_template, request, redirect, url_for from flask_sqlalchemy import SQLAlchemy import config app = Flask(__name__) app.config.from_object(config) db = SQLAlchemy(app) class Record(db.Model): __tablename__ = 'record' id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String(100), nullable=False) content = db.Column(db.Text, nullable=False) @app.route('/') def index(): return render_template('index.html') @app.route('/sign_up', methods=['GET', 'POST']) def add_user(): if request.method == 'GET': return render_template('sign_up.html') else: for i in request.args: print(i) # print(request.args.get('username')) #None name = request.form.get('name') print("name={}".format(name)) content = request.form.get('content') print("content={}".format(content)) record = Record(name=name, content=content) db.session.add(record) db.session.commit() return redirect(url_for('index')) if __name__ == '__main__': db.init_app(app) app.run()
config.py
sql
SQLALCHEMY_TRACK_MODIFICATIONS=True DIALECT = 'mysql' DRIVER = 'pymysql' USERNAME = 'root' PASSWORD = 'hujin666..' HOST = '127.0.0.1' PORT = '3306' DATABASE = 'socketio_test' SQLALCHEMY_DATABASE_URI = "{}+{}://{}:{}@{}:{}/{}?charset=utf8".format(DIALECT, DRIVER, USERNAME, PASSWORD, HOST, PORT, DATABASE)
index.html
數據庫
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>首頁</title> </head> <body> <a href="/sign_up">註冊</a> </body> </html>
sign_up.html
flask
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>註冊</title> </head> <body> <form action="/sign_up" method="post"> <label for="name">用戶名</label><input type="text" name="name" id="name"> <label for="content">密 碼</label><input type="text" name="content" id="content"> <input type="submit" value="提交"> </form> </body> </html>
首先咱們在根目錄中新建一個extensions.py
,將app.py
中的頭部部分拿進去extensions.py
session
from flask import Flask from flask_sqlalchemy import SQLAlchemy import config app = Flask(__name__) app.config.from_object(config) db = SQLAlchemy(app)
而後在根目錄中新建models包,並在models裏面新建record.py
文件record.py
app
from extensions import db class Record(db.Model): __tablename__ = 'record' id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String(100), nullable=False) content = db.Column(db.Text, nullable=False)
接着在根目錄下新建routes包,並在routes裏面新建record.py
的路由文件socket
from extensions import app,db from flask import render_template, request, redirect, url_for from models.record import Record @app.route('/') def index(): return render_template('index.html') @app.route('/sign_up', methods=['GET', 'POST']) def add_user(): if request.method == 'GET': return render_template('sign_up.html') else: for i in request.args: print(i) # print(request.args.get('username')) #None name = request.form.get('name') print("name={}".format(name)) content = request.form.get('content') print("content={}".format(content)) record = Record(name=name, content=content) db.session.add(record) db.session.commit() return redirect(url_for('index'))
最後在根目錄中新建一個入口文件run.py
post
run.py
from extensions import db from routes.record import app if __name__ == '__main__': db.init_app(app) app.run()
目錄結構
數據庫初始化比較簡單,請自行建庫建表,修改鏈接配置自此大功告成,邁過flask的第一大坑