来自不同目录的瓶子服务文件

2024-09-26 22:55:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个简单的瓶子应用程序,为/开始页面位置服务。这个索引.html页面位于/banana文件夹下,banana文件夹位于视图.py存在。在

当我尝试此操作时,它无法找到页面并引发内部服务器错误

@app.wrap_app.route('/StartPage',method='GET')
def doStartPage():
    return template('banana/index.html')

如何在模板中引用/banana文件夹?在


Tags: py服务器文件夹视图app应用程序瓶子html
2条回答

瓶子FAQ指定以下内容

Bottle searches in ./ and ./views/ for templates. In a mod_python or mod_wsgi environment, the working directory (./) depends on your Apache settings. You should add an absolute path to the template search path so bottle searches the right paths.

您需要将bananas文件夹添加到TEMPLATE_PATH

base_path = os.path.abspath(os.path.dirname(__file__))
bananas_path = os.path.join(base_path, 'bananas')
bottle.TEMPLATE_PATH.insert(0, bananas_path)

编辑:用Graham的建议改进了答案,即使用相对于代码所在位置的路径。在

你可以这样做:

为文件夹创建根规则:

@route('/banana/<filepath:path>')
def file_stac(filepath):
    return static_file(filepath, root="./banana")

然后,您只需要像这样引用这个文件夹:

^{pr2}$

您可以对任意多个文件夹执行相同的操作,这对于在模板中提供css/js文件非常有用(在您的模板上,您可以执行以下操作:

<link href="banana/css/bootstrap.min.css" rel="stylesheet">

希望有帮助!在

相关问题 更多 >

    热门问题