如何能使每一个跳转都是一个绝对的url,这里需要使用url管理器。

    在common/lib下新建一个UrlManager.py,定义一个UrlManager的类:

    1. # _*_ coding:utf-8 _*_
    2. from application import app
    3. class UrlManager():
    4. @staticmethod
    5. def builder(path):
    6. config_domain = app.config['DOMAIN']
    7. return "%s%s"%(config_domain['www'],path)
    8. @staticmethod
    9. def buildStaticUrl(path):
    10. path = "/static"+path
    11. return UrlManager.builder(path)

    这里的config信息需要在配置文件中添加一下,因为不同的环境可能存在不一样的情况,这里在local的配置文件中添加一下:但是实际上我这里在local_setting.py 加了并没有用,是Windows环境,提示找不到这个环境变量,我是把环境变量的ops_config定义为了local了。

    1. # _*_ coding:utf-8 _*_
    2. from base_setting import *
    3. # 下面自由发挥
    4. DOMAIN = {
    5. 'www':'http://127.0.0.1:5000'
    6. }

    然后,需要在注册文件register.py中添加相关信息,这里使用到了全局函数add_template_global

    1. from controllers.index import index_page
    2. from controllers.member import member_page
    3. from application import app
    4. from interceptor.auth import *
    5. from interceptor.errorhandler import *
    6. # from flask_debugtoolbar import DebugToolbarExtension
    7. # toolbar = DebugToolbarExtension(app)
    8. app.register_blueprint(index_page,url_prefix='/learning')
    9. app.register_blueprint(member_page,url_prefix='/member')
    10. from controllers.UrlManager import UrlManager
    11. app.add_template_global(UrlManager.builderUrl,'builderUrl')
    12. app.add_template_global(UrlManager.buildStaticUrl,'buildStaticUrl')

    接着,需要去静态文件中把相关的链接也都替换掉

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <!-- Required meta tags -->
    5. <meta charset="utf-8">
    6. <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    7. <link rel="stylesheet" type="text/css" href="{{buildStaticUrl('/plugins/bootstrap/css/bootstrap.css')}}">
    8. <!-- Bootstrap CSS -->
    9. </head>
    10. <body>
    11. <nav class="navbar navbar-inverse" style="border-radius: 0px">
    12. <div class="container-fluid">
    13. <!-- Brand and toggle get grouped for better mobile display -->
    14. <div class="navbar-header">
    15. <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
    16. <span class="sr-only">Toggle navigation</span>
    17. <span class="icon-bar"></span>
    18. <span class="icon-bar"></span>
    19. <span class="icon-bar"></span>
    20. </button>
    21. <a class="navbar-brand" href="{{builderUrl('/')}}">猫影</a>
    22. </div>
    23. <!-- Collect the nav links, forms, and other content for toggling -->
    24. <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
    25. <ul class="nav navbar-nav">
    26. <li class="active"><a href="{{builderUrl('/')}}">影视<span class="sr-only">(current)</span></a></li>
    27. </ul>
    28. <ul class="nav navbar-nav navbar-right">
    29. <li><a href="{{builderUrl('/member/reg')}}">注册</a></li>
    30. <li><a href="{{builderUrl('/member/login')}}">登录</a> </li>
    31. </ul>
    32. </div><!-- /.navbar-collapse -->
    33. </div><!-- /.container-fluid -->
    34. </nav>
    35. <div class="container" style="min-height: 600px">
    36. {% block content %}
    37. {% endblock%}
    38. </div>
    39. <footer class="text-center">
    40. Copy right Juha
    41. </footer>
    42. <script type="text/javascript" src="{{buildStaticUrl('/plugins/jquery/jquery-3.5.1.min.js')}}"></script>
    43. </body>
    44. </html>

    这里很好的一点,就是把链接可以替换为绝对的路径,不用担心改一个而改全部的问题了。
    image.png