【问题标题】:Display html pages containing css, jpg using Bottle使用 Bottle 显示包含 css、jpg 的 html 页面
【发布时间】:2015-08-13 11:35:57
【问题描述】:

我有大约 100 篇期刊文章。每篇期刊文章都表示为一个 HTML 页面。期刊的 HTML 页面、其 css 文件和属于该期刊一部分的图表位于其文件夹中。换句话说,每个 html 文件都是指它自己的 css 文件和相应的图像文件。并且我创建了以下文件夹结构(| 代表一个文件夹,-- 代表一个文件):

-- app.py |views -- Disp_Res.tpl -- UI.tpl |static |articles |PMC45677 --PMC45677.html --jats-preview.css --fig1_45677.jpg --fig2_45677.jpg |PMC23456 --PMC23456.html --jats-preview.css --fig1_23456.jpg --fig2_23456.jpg

我的 app.py 中有以下代码

@app.get('/articles/<pmc:re:PMC[0-9]*>')
def html_article(pmc):    
    global pmcno
    pmcno = pmc; 
    pmc_article = pmcno + ".html"; print pmc_article        
    rootdir = os.path.join('static/articles', pmcno) #'static/articles/{pmcno}'
    print "html_article", rootdir # This statement displays the right dir
    return static_file(pmc_article, root=rootdir)


@app.get('/<filename:re:.*\.css>')
def stylesheets(filename):
    rootdir = os.path.join('static/articles', pmcno)
    print "stylesheets", rootdir # This statement does NOT display
    return static_file(filename, root=rootdir)


@app.get('/<filename:re:.*\.(jpg|png|gif|ico)>')
def images(filename):
    rootdir = os.path.join('static/articles', pmcno)
    print "images", rootdir # This statement does NOT display
    return static_file(filename, root=rootdir)

不用说这是行不通的。当我运行 app.py 时,它只会给我一个错误: “抱歉,请求的 URL 'http://localhost:8080/articles/PMC45677' 导致错误: 文件不存在。”

知道我做错了什么吗?还是您对实现我的目标有更好的想法?请务必让我知道。任何帮助将不胜感激!

提前致谢。

【问题讨论】:

    标签: python html css bottle


    【解决方案1】:

    也许是因为 Bottle 不知道 /articles/ 是什么?

    尝试将其添加到您的代码顶部:

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

    【讨论】:

      猜你喜欢
      • 2015-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-10
      • 1970-01-01
      相关资源
      最近更新 更多