1. pip list
Flask 0.10.1 Flask-Bootstrap 3.3.5.6 Flask-SQLAlchemy 2 Flask-Script 2.0.5 Flask-WTF 0.12 flask-nav 0.4 Jinja2 2.7.3 Markdown 2.6.2 MarkupSafe 0.23 SQLAlchemy 1.0.8 WTForms 2.0.2 Werkzeug 0.10.4 dominate 2.1.12 itsdangerous 0.24 visitor 0.1.2
2. 布局后的文件结构(怎么生成的,cmd进入目录:C:UsersAdministratorPycharmProjectsSample>tree /f>list.txt)
│ list.txt │ manager.py │ ├─.idea │ Sample.iml │ misc.xml │ modules.xml │ workspace.xml │ ├─app │ │ config │ │ forms.py │ │ models.py │ │ views.py │ │ __init__.py #原Sample.py │ │ │ ├─static │ │ └─uploads │ │ │ └─templates │ │ base.html │ │ home.html │ │ index.html │ │ login.html │ │ upload.html │ │ _macro.html │ │ │ └─includes │ _head.html │ └─doc http_methods.md
app文件夹下的才是程序本身,外部的manager.py只是项目开发测试阶段使用的
#__init__.py(原Sample.py)
# coding:utf-8 from flask import Flask from flask_bootstrap import Bootstrap from flask_nav import Nav from flask_nav.elements import * from os import path from .views import init_views from flask_sqlalchemy import SQLAlchemy basedir = path.dirname(__file__) bootstrap = Bootstrap() nav = Nav() db = SQLAlchemy() def create_app(): app = Flask(__name__) app.config.from_pyfile('config') app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + path.join(basedir, 'data.sqlite') app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True nav.register_element('top', Navbar(u'Flask入门', View(u'主页', 'home'), View(u'关于', 'about'), Subgroup(u'项目', View(u'项目一', 'about'), Separator(), View(u'项目二', 'service'), ), )) bootstrap.init_app(app) nav.init_app(app) db.init_app(app) init_views(app) return app
#views.py
# coding:utf-8 from flask import render_template, request, url_for, flash def init_views(app): @app.route('/') def home(): return render_template('home.html', title_name='welcome') @app.route('/service') def service(): return 'service' @app.route('/about') def about(): return 'about' @app.template_test('current_link') def is_current_link(link): return link == request.path @app.route('/login', methods=['POST', 'GET']) def login(): from forms import LoginForm form = LoginForm() flash(u'登陆成功') return render_template('login.html', title=u'登录', form=form)
#forms.py
#coding:utf-8 from flask.ext.wtf import Form from wtforms import StringField,PasswordField,SubmitField from wtforms.validators import DataRequired class LoginForm(Form): username = StringField(label=u'用户名',validators=[DataRequired()]) password = PasswordField(label=u'密码',validators=[DataRequired()]) submit = SubmitField(label=u'提交')
#models.py
from . import db class Role(db.Model): __tablename__ = 'roles' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String, nullable=True) users = db.relationship('User', backref='role') class User(db.Model): __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String, nullable=True) password = db.Column(db.String, nullable=True) role_id = db.Column(db.Integer,db.ForeignKey('roles.id'))
#manager.py
# coding:utf-8 from werkzeug.utils import secure_filename from flask.ext.script import Manager from app import create_app app = create_app() manager = Manager(app) @manager.command def dev(): from livereload import Server live_server = Server(app.wsgi_app) live_server.watch('**/*.*') live_server.serve(open_url=True) # 自己打开浏览器 if __name__ == '__main__': manager.run()