代码之家  ›  专栏  ›  技术社区  ›  Natalie Perret

静态文件的路由问题

  •  1
  • Natalie Perret  · 技术社区  · 6 年前

    你好,我的静态文件(示例: <script type="text/javascript" src="js/main.js"></script> 在我的标题中[partial template])不能在我不完全 localhost:4242 :

    • http://localhost:4242/ :有效
    • http://localhost:4242/samples :不工作,请使用当前位置连接 js/main.js

    我的文件是这样组织的:

    • /runserver.py
    • /project/static/js/main.js

    我有以下应用程序和 static_file 使用:

    static.py

    from project.libs.bottle import static_file
    from runserver import app
    
    
    # (Agnostic) Static Routes
    @app.get('/<filename:path>')
    def send_static(filename):
        return static_file(filename, root='project/static/')
    

    runserver.py

    import os
    import sys
    
    from project.libs.bottle import Bottle, run, TEMPLATE_PATH, default_app
    
    app = default_app()
    
    dir_name = os.path.dirname(sys.argv[0])
    
    TEMPLATE_PATH.insert(0, './project/views/layout')
    TEMPLATE_PATH.insert(1, './project/views')
    
    import project.routes.static
    import project.routes.business
    import project.routes.error
    
    if __name__ == '__main__':
        run(host="localhost", port=4242, debug=True, reloader=True)
    
    2 回复  |  直到 6 年前
        1
  •  0
  •   jspcal    6 年前

    这个 root 通往 static_file 应该是绝对路径而不是相对路径,因为工作目录可能与项目目录不同。

    return static_file(filename, root='project/static/')
    

    此路径也匹配所有路径

    @app.get('/<filename:path>')
    
        2
  •  0
  •   Natalie Perret    6 年前

    好的,考虑到jspcal提到的绝对路径(在实际部署中更好,例如Azure),我设法创建了一个工作解决方案。

    保持相同的结构(文件):

    static.py

    # (Agnostic-ish) Static Routes
    @app.get('/:path#(css|favicons|fonts|img|js)\/.+#')
    def send_static(path):
        return static_file(path, root=STATIC_ROOT)
    

    runserver.py

    """
    This script runs the application using a development server.
    """
    
    import os
    import sys
    
    from project.libs.bottle import run, TEMPLATE_PATH, default_app, debug
    
    app = default_app()
    
    TEMPLATE_PATH.insert(0, './project/views/layout')
    TEMPLATE_PATH.insert(1, './project/views')
    
    # routes contains the HTTP handlers for our server and must be imported.
    import project.routes.static
    import project.routes.business
    import project.routes.error
    
    if '--debug' in sys.argv[1:] or 'SERVER_DEBUG' in os.environ:
        # Debug mode will enable more verbose output in the console window.
        # It must be set at the beginning of the script.
        debug(True)
    
    
    def wsgi_app():
        """Returns the application to make available through wfastcgi. This is used
        when the site is published to Microsoft Azure."""
        return app
    
    
    PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
    STATIC_ROOT = os.path.join(PROJECT_ROOT, 'project/static').replace('\\', '/')
    HOST = os.environ.get('SERVER_HOST', 'localhost')
    try:
        PORT = int(os.environ.get('SERVER_PORT', '5555'))
    except ValueError:
        PORT = 5555
    
    
    if __name__ == '__main__':
        run(host=HOST, port=PORT, debug=True, reloader=True)
    

    我的静态文件“导入”:

    <script type="text/javascript" src="/js/main.js"></script>
    

    信贷:部分灵感来自Github上的MS回购: https://github.com/Microsoft/project-python-bottle-webapp