python基於flask模組
阿新 • • 發佈:2018-11-11
樹形結構
./ ├── app │ ├── config.py │ ├── __init__.py │ ├── modus │ │ ├── froms.py │ │ └── __init__.py │ ├── templates │ │ ├── auth │ │ │ └── index.html │ │ ├── base.html │ │ ├── error.html │ │ ├── index.html │ │ └── message.html │ └── views │ ├── auth.py │ ├── common.py │ ├── index.py │ ├── __init__.py └── run.py
PYTHON 檔案
run.py
# -*- coding: utf-8 -*- from app import app if __name__ == '__main__': app.run(threaded=True, host='0.0.0.0', port=80)
app/config.py
# -*- coding: utf-8 -*- DEBUG = True CSRF_ENABLED = True SECRET_KEY = 'environment' USERNAME = 'admin' PASSWD = '123456'
app/__init__.py
# -*- coding: utf-8 -*- from flask import Flask from flask_wtf.csrf import CSRFProtect # 初始化Flask app = Flask(__name__) # 載入配置檔案 app.config.from_pyfile('config.py') # 載入CSRF保護模組 csrf = CSRFProtect() csrf.init_app(app) # from app.views import auth, index app.register_blueprint(auth.BluLogin) app.register_blueprint(index.BluIndex)
app/views/auth.py
# -*- coding: utf-8 -*- from app.views.common import * # 定義藍圖 BluLogin = Blueprint('BluLogin', __name__, url_prefix='/login') @BluLogin.route('', methods=('GET', 'POST')) def login(): form = froms.LoginForm() if form.validate_on_submit(): username = form.username.data passwd = form.password.data if username == app.config['USERNAME'] and passwd == app.config['PASSWD']: session['username'] = username session['passwd'] = passwd return redirect(url_for('BluIndex.index')) else: flash(u'使用者名稱密碼錯誤','error') return redirect(url_for('BluLogin.login')) return render_template("auth/index.html", form=form) @BluLogin.route('/logout', methods=('GET', 'POST')) def logout(): session.pop('username', None) session.pop('passwd', None) return redirect(url_for('BluLogin.login'))
app/views/common.py
# -*- coding: utf-8 -*- from app.modus import froms from flask import Blueprint, render_template, session, redirect, url_for, flash, g from flask_wtf.csrf import CSRFError from functools import wraps from app import app # 使用者登入修飾器 def UserLoginModifier(func): @wraps(func) def decorated_view(*args, **kwargs): if session.get('username') == app.config['USERNAME'] and session.get('passwd') == app.config['PASSWD']: return func(*args, **kwargs) else: flash(u'請先登入,在進行操作!') return redirect(url_for('BluLogin.login')) return decorated_view @app.errorhandler(CSRFError) def csrf_error(reason): return render_template("error.html",reason=reason) , 400
app/views/index.py
# -*- coding: utf-8 -*- from app.views.common import * # 定義藍圖 BluIndex = Blueprint('BluIndex', __name__, url_prefix='/') @BluIndex.route('', methods=('GET', 'POST')) @UserLoginModifier def index(): g.username = session.get('username') return render_template("index.html")
app/modus/froms.py
# -*- coding: utf-8 -*- from flask_wtf import FlaskForm from wtforms import StringField, PasswordField, SubmitField from wtforms.validators import DataRequired class LoginForm(FlaskForm): username = StringField(u'使用者', validators=[DataRequired()]) password = PasswordField(u'密碼', validators=[DataRequired()]) submit = SubmitField(u'登入')
HTML 檔案
app/templates/index.html
{% extends "base.html" %} <!--繼承--> <!--修改父模組內容--> {% block title -%} 首頁 {% endblock -%} {% block content -%} 歡迎 <a class="bases">{{ session.get('username') }}</a> <p><a href="{{ url_for('BluLogin.logout') }}"> 登出 </a></p> {% endblock -%} {% block footer -%} {{ super() -}} <!--super() 追加 --> <p>登入之後顯示 , 版權所有</p> {% endblock -%}
app/templates/base.html
<!DOCTYPE html> <html lang="en"> <head> <!--定義模組--> {% block head -%} <style type="text/css"> .bases { color: #cc0000; } .error { color: red} </style> <!--定義模組--> <title> {% block title -%} {% endblock -%}</title> {% endblock -%} </head> <body> <div id="content"> <!--定義模組--> {% block content -%} {% endblock -%} </div> <!-- 包含子頁 --> {% include "message.html" -%} <div id="footer"> {% block footer -%} <!--定義模組--> <a class="bases">Copyright 2018V</a> {% endblock -%} </div> </body> </html>
app/templates/message.html
{% with messages = get_flashed_messages(with_categories=true) -%} {% if messages -%} <ul class=flashes> {% for category, message in messages -%} <li class="{{ category }}">{{ message }}</li> {% endfor -%} </ul> {% endif -%} {% endwith -%}
app/templates/error.html
{% extends "base.html" %} <!--繼承--> <!--修改父模組內容--> {% block title -%} 請求錯誤 {% endblock -%} {% block content -%} <p>{{ reason }} </p> <p><a href="{{ url_for('BluIndex.index') }}"> 返回首頁 </a></p> {% endblock -%}
app/templates/auth/index.html
{% extends "base.html" %} <!--繼承--> <!--修改父模組內容--> {% block title -%} 使用者登入 {% endblock -%} {% block head -%} {{ super() -}} <!--super() 追加 --> <style type="text/css"> .important { color: #336699; } </style> {% endblock -%} {% block content -%} <form method="POST" action="" name="login"> {{ form.csrf_token }} <p>{{ form.username.label }} {{ form.username(size=20) }}</p> <p>{{ form.password.label }} {{ form.password(size=20) }}</p> <p>{{ form.submit }}</p> </form> {% endblock -%}
未登入訪問:http://127.0.0.1 會重定向到登入頁面