为了账号安全,请及时绑定邮箱和手机立即绑定

【九月打卡】第8天+点餐系统后台界面搭建

标签:
Flask

https://coding.imooc.com/learn/list/265.html


课程名称:Python Flask构建微信小程序订餐系统


课程章节:第五章 点餐系统后台界面搭建——5-1 管理员登录和列表页面



主讲老师:编程浪子




5-1 管理员登录和列表页面

    1.后台管理员账户相关页面

    登录界面效果展示:

    https://img1.sycdn.imooc.com//631f355f0001fb7b19201030.jpg

     

    后台管理员登录操作界面展示:

    https://img1.sycdn.imooc.com//631f356c000176ec19201030.jpg

     

     

     

     

     

    2.账户登录相关界面

    https://img1.sycdn.imooc.com//631f357d0001059616491019.jpg

    1.准备工作(实现本机与Linux虚拟机文件共享)

    mount -t vboxsf Google /home/www/

    https://img1.sycdn.imooc.com//631f35880001b71119201030.jpg

     

    2.打开imooc虚拟环境

    source imooc/bin/activate(在~目录下才能打开)

    https://img1.sycdn.imooc.com//631f35940001993a19201030.jpg

     

    3.运行manager.py文件——python manager.py runserver

    https://img1.sycdn.imooc.com//631f35a10001901d19201030.jpg

     

     

       指定使用哪个config——export ops_config=local

    https://img1.sycdn.imooc.com//631f35b80001ae7919201030.jpg

    设置配置文件代码:

    https://img1.sycdn.imooc.com//631f35c9000182b011710667.jpg

           https://img1.sycdn.imooc.com//631f35da0001bb5713660480.jpg

    

     

     

    4.管理员登录

    https://img1.sycdn.imooc.com//631f35f200017c7013820926.jpg

     

    运行登录管理员登录界面(无法登录)

    https://img1.sycdn.imooc.com//631f35fd00016d6313740517.jpg

     

    显示404原因——所有蓝图注入都需要在www.py文件中注入

    https://img1.sycdn.imooc.com//631f3607000175af10710758.jpg

     

    再次运行蓝图注入好的文件:

    https://img1.sycdn.imooc.com//631f361100013cbc08410310.jpg

     

    5.使用login.html渲染登录页面

    from flask import render_template

    def login():

        # render_template()方法——页面渲染   参数—传入指定html文件

        return render_template("user/login.html")

    https://img1.sycdn.imooc.com//631f36220001813012560867.jpg

    https://img1.sycdn.imooc.com//631f362f0001c60119201030.jpg

    

     

    运行结果:

    https://img1.sycdn.imooc.com//631f36400001fa0819201030.jpg

     

     

    6.指定templates指定文件夹位置

    原因:application.py文件继承的app.py模块的__init__()构造函数,template_folder指定位置,因此需要重新指定位置

    https://img1.sycdn.imooc.com//631f364d000145fa16730885.jpg

     

    application.py模块中重新指定template_folder位置

    注:os.getcwd()——获取当前目录

    https://img1.sycdn.imooc.com//631f36610001a52b16520844.jpg

     

    重新运行manager.py

    错误——exceptions.UndefinedError: 'buildStaticUrl' is undefined

    https://img1.sycdn.imooc.com//631f366b00019d4f19201029.jpg

     

     

    7.exceptions.UndefinedError: 'buildStaticUrl' is undefined问题解决

    1.错误源头——layout_user.html中找不到buildStaticUrl()方法:

    https://img1.sycdn.imooc.com//631f36760001347c15480822.jpg

     

    2.UrlManager.py模块——否则将始终无法加载出html文件和静态文件

    https://img1.sycdn.imooc.com//631f3687000109c719201030.jpg

     

    3.如何使得HTML这些模板文件中也可以调用buildStaticUrl()方法

    '''

        函数模板——使得html文件也能够调用buildStaticUrl()函数

        注:使得python的函数能够注入到html中使用

    '''

    from common.libs.UrlManager import UrlManager

    app.add_template_global(UrlManager.buildStaticUrl, 'buildStaticUrl')

    app.add_template_global(UrlManager.buildUrl, 'buildUrl')

    app.add_template_global(UrlManager.buildImageUrl, 'buildImageUrl')

    # 将两个函数注入到模板内

         

    

     

    4.再次运行manager.py

    新问题——无法加载静态文件

    

     

     

     

    8.静态文件无法加载问题

    原因:1.Flask的__init__()方法需要指定static_folder值

    2.定义static.py处理无法加载静态文件

    管理员登录最终运行结果:

    



 



点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消