Flask入门小项目 - 搭建极简博客(1)介绍与项目结构
目录:
一、介绍
功能:未登录可以浏览文章、注册账户,登录后还可以写文章、删除文章
效果:
主要参考了这里:https://www.shiyanlou.com/courses/29
二、项目结构
app.py: 主程序
config.py: 配置文件
models.py: 数据库模型
manage.py: 数据库管理style.css: 样式表
base.html: 网站基本结构,底下那些html都是继承它的
index.html: 主页,展示文章
login.html: 登录
signup.html: 注册
post.html: 写文章,需登录才能访问
delete.html: 删除文章,需登录才能访问
- 1.pycharm中新建flask工程
- 2.主文件夹中新建config.py, models.py, manage.py
- 3.templates文件夹中新建layout.html, login.html, show_entries.html
- 4.static文件夹中新建style.css
- 5.migrations文件夹是数据库迁移的产物,具体怎么来的下面再说
安装依赖包:在pycharm底下的Terminal中输入
pip install flask-script
pip install flask-migrate
pip install flask-login
pip install pymysql
三、配置数据库
本地MySQL数据库中新建一个空的schema,这里取名叫blog
然后修改app.py, config.py, models.py, manage.py:
# app.py
from flask import Flask, render_template, flash, request, session, redirect, url_for, abort
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager, login_required, login_user, logout_user
app = Flask(__name__)
app.config.from_pyfile('config.py')
db = SQLAlchemy(app)
from models import *
@app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == '__main__':
app.run(debug=True)
# config.py
import os
SECRET_KEY = os.urandom(24)
DIALECT = 'mysql'
DRIVER = 'pymysql'
USERNAME = 'root' # 改为自己的数据库用户名
PASSWORD = 'root' # 改为自己的数据库密码
HOST = '127.0.0.1'
PORT = '3306'
DATABASE = 'blog' # 改为自己新建的schema名
SQLALCHEMY_DATABASE_URI = "{}+{}://{}:{}@{}:{}/{}?charset=utf8".format(DIALECT, DRIVER, USERNAME, PASSWORD, HOST, PORT, DATABASE)
SQLALCHEMY_TRACK_MODIFICATIONS = False
# models.py
from app import db
from flask_login import UserMixin
class Article(db.Model):
__tablename__ = 'articles'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
title = db.Column(db.String(100), nullable=False)
text = db.Column(db.Text, nullable=False)
class User(db.Model, UserMixin):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
username = db.Column(db.String(100), nullable=False, unique=True)
password = db.Column(db.Text, nullable=False)
def __init__(self, id=None, username=None, password=None):
self.id = id
self.username = username
self.password = password
# manage.py
from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand
from app import app, db
from models import *
manager = Manager(app)
migrate = Migrate(app, db)
manager.add_command('db', MigrateCommand)
if __name__ == '__main__':
manager.run()
接着在pycharm底下的Terminal中输入
python manage.py db init # 生成文件夹migrations
python manage.py db migrate # 把models模型生成迁移文件
python manage.py db upgrade # 从migrations文件夹迁移到数据库
这时看到数据库中多了几个表,就是models里的那些
如果要将数据库降回到上一个状态可以输入python manage.py db downgrade
四、style.css
body {
font-family: sans-serif;
background: #eee;
}
a,
h1,
h2 {
color: #377ba8;
}
h1,
h2 {
font-family: "Georgia", serif;
margin: 0;
}
h1 {
border-bottom: 2px solid #eee;
}
h2 {
font-size: 1.2em;
}
.page {
margin: 2em auto;
width: 35em;
border: 5px solid #ccc;
padding: 0.8em;
background: white;
}
.entries {
list-style: none;
margin: 0;
padding: 0;
}
.entries li {
margin: 0.8em 1.2em;
}
.entries li h2 {
margin-left: -1em;
}
.add-entry {
font-size: 0.9em;
border-bottom: 1px solid #ccc;
}
.add-entry dl {
font-weight: bold;
}
.metanav {
text-align: right;
font-size: 0.8em;
padding: 0.3em;
margin-bottom: 1em;
background: #fafafa;
}
.flash {
background: #cee5f5;
padding: 0.5em;
border: 1px solid #aacbe2;
}
.error {
background: #f0d6d6;
padding: 0.5em;
}