1. 程式人生 > >flask建部落格步驟1

flask建部落格步驟1

連線資料庫,
配置資訊
設定部落格導航欄
匯入 flask-sqlalchemy,flask-script,,flask-migrate模組

1:app.py程式碼

from flask import Flask,render_template
import  config

app = Flask(__name__)
app.config.from_object(config)

@app.route('/')
def index():
    return render_template('index.html')

# @app.route('/user/<name>')
# def user(name):
#     return render_template('user.html', name=name)

if __name__ == '__main__':
    app.run()

2:配置檔案config.py

import os

DEBUG = True

SECRET_KEY = os.urandom(24)

DB_URI ='mysql+mysqldb://{}:{}@{}:{}/{}?charset=utf-8'.format(USERNAME,PASSWORD,HOSTNAME,PORT,DATABASE)
SQLALCHEMY_DATABASE_URL =DB_URI

3:exts.py

from flask_sqlalchemy import  SQLAlchemy

db = SQLAlchemy()

4:manage.py檔案

from flask_script import Manager
from flask_migrate import Migrate,MigrateCommand
from app import app
from exts import db

manager = Manager(app)

#使用Migrate 繫結app和db
migrate = Migrate(app,db)

#新增遷移指令碼的命令到manager中
manager.add_command('db',MigrateCommand)

if __name__ == '__main__':
    manager.run()

5:models.py

from exts import db