python基于flask模块-创新互联
树形结构
成都创新互联是一家集网站建设,临泽企业网站建设,临泽品牌网站建设,网站定制,临泽网站建设报价,网络营销,网络优化,临泽网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。./ ├── 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 -%} 欢迎 {{ session.get('username') }} {% endblock -%} {% block footer -%} {{ super() -}}登录之后显示 , 版权所有
{% endblock -%}
app/templates/base.html
{% block head -%}{% block title -%} {% endblock -%} {% endblock -%}{% block content -%} {% endblock -%}{% include "message.html" -%}{% block footer -%} Copyright 2018V {% endblock -%}
app/templates/message.html
{% with messages = get_flashed_messages(with_categories=true) -%} {% if messages -%}
-
{% for category, message in messages -%}
- {{ message }} {% endfor -%}
app/templates/error.html
{% extends "base.html" %} {% block title -%} 请求错误 {% endblock -%} {% block content -%}{{ reason }}
{% endblock -%}
app/templates/auth/index.html
{% extends "base.html" %} {% block title -%} 用户登录 {% endblock -%} {% block head -%} {{ super() -}} {% endblock -%} {% block content -%}{% endblock -%}
未登录访问:http://127.0.0.1 会重定向到登录页面
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
名称栏目:python基于flask模块-创新互联
URL分享:http://scyanting.com/article/cegeid.html