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-*-fromappimportappif__name__=='__main__':app.run(threaded=True,host='0.0.0.0',port=80)
app/config.py
#-*-coding:utf-8-*-DEBUG=TrueCSRF_ENABLED=TrueSECRET_KEY='environment'USERNAME='admin'PASSWD='123456'
app/__init__.py
#-*-coding:utf-8-*-fromflaskimportFlaskfromflask_wtf.csrfimportCSRFProtect#初始化Flaskapp=Flask(__name__)#加载配置文件app.config.from_pyfile('config.py')#加载CSRF保护模块csrf=CSRFProtect()csrf.init_app(app)#fromapp.viewsimportauth,indexapp.register_blueprint(auth.BluLogin)app.register_blueprint(index.BluIndex)
app/views/auth.py
#-*-coding:utf-8-*-fromapp.views.commonimport*#定义蓝图BluLogin=Blueprint('BluLogin',__name__,url_prefix='/login')@BluLogin.route('',methods=('GET','POST'))deflogin():form=froms.LoginForm()ifform.validate_on_submit():username=form.username.datapasswd=form.password.dataifusername==app.config['USERNAME']andpasswd==app.config['PASSWD']:session['username']=usernamesession['passwd']=passwdreturnredirect(url_for('BluIndex.index'))else:flash(u'用户名密码错误','error')returnredirect(url_for('BluLogin.login'))returnrender_template("auth/index.html",form=form)@BluLogin.route('/logout',methods=('GET','POST'))deflogout():session.pop('username',None)session.pop('passwd',None)returnredirect(url_for('BluLogin.login'))
app/views/common.py
#-*-coding:utf-8-*-fromapp.modusimportfromsfromflaskimportBlueprint,render_template,session,redirect,url_for,flash,gfromflask_wtf.csrfimportCSRFErrorfromfunctoolsimportwrapsfromappimportapp#用户登录修饰器defUserLoginModifier(func):@wraps(func)defdecorated_view(*args,**kwargs):ifsession.get('username')==app.config['USERNAME']andsession.get('passwd')==app.config['PASSWD']:returnfunc(*args,**kwargs)else:flash(u'请先登录,在进行操作!')returnredirect(url_for('BluLogin.login'))returndecorated_view@app.errorhandler(CSRFError)defcsrf_error(reason):returnrender_template("error.html",reason=reason),400
app/views/index.py
#-*-coding:utf-8-*-fromapp.views.commonimport*#定义蓝图BluIndex=Blueprint('BluIndex',__name__,url_prefix='/')@BluIndex.route('',methods=('GET','POST'))@UserLoginModifierdefindex():g.username=session.get('username')returnrender_template("index.html")
app/modus/froms.py
#-*-coding:utf-8-*-fromflask_wtfimportFlaskFormfromwtformsimportStringField,PasswordField,SubmitFieldfromwtforms.validatorsimportDataRequiredclassLoginForm(FlaskForm):username=StringField(u'用户',validators=[DataRequired()])password=PasswordField(u'密码',validators=[DataRequired()])submit=SubmitField(u'登录')
HTML 文件
app/templates/index.html
{%extends"base.html"%}<!--继承--><!--修改父模块内容-->{%blocktitle-%}首页{%endblock-%}{%blockcontent-%}欢迎<aclass="bases">{{session.get('username')}}</a><p><ahref="{{url_for('BluLogin.logout')}}">登出</a></p>{%endblock-%}{%blockfooter-%}{{super()-}}<!--super()追加--><p>登录之后显示,版权所有</p>{%endblock-%}
app/templates/base.html
<!DOCTYPEhtml><htmllang="en"><head><!--定义模块-->{%blockhead-%}<styletype="text/css">.bases{color:#cc0000;}.error{color:red}</style><!--定义模块--><title>{%blocktitle-%}{%endblock-%}</title>{%endblock-%}</head><body><divid="content"><!--定义模块-->{%blockcontent-%}{%endblock-%}</div><!--包含子页-->{%include"message.html"-%}<divid="footer">{%blockfooter-%}<!--定义模块--><aclass="bases">Copyright2018V</a>{%endblock-%}</div></body></html>
app/templates/message.html
{%withmessages=get_flashed_messages(with_categories=true)-%}{%ifmessages-%}<ulclass=flashes>{%forcategory,messageinmessages-%}<liclass="{{category}}">{{message}}</li>{%endfor-%}</ul>{%endif-%}{%endwith-%}
app/templates/error.html
{%extends"base.html"%}<!--继承--><!--修改父模块内容-->{%blocktitle-%}请求错误{%endblock-%}{%blockcontent-%}<p>{{reason}}</p><p><ahref="{{url_for('BluIndex.index')}}">返回首页</a></p>{%endblock-%}
app/templates/auth/index.html
{%extends"base.html"%}<!--继承--><!--修改父模块内容-->{%blocktitle-%}用户登录{%endblock-%}{%blockhead-%}{{super()-}}<!--super()追加--><styletype="text/css">.important{color:#336699;}</style>{%endblock-%}{%blockcontent-%}<formmethod="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会重定向到登录页面
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。