【问题标题】:Loading a webpage into iFrame Flask [duplicate]将网页加载到 iFrame Flask [重复]
【发布时间】:2017-02-14 13:01:35
【问题描述】:

我对 Flask 真的很陌生。我想在网络上创建一个移动应用程序模拟。我找到了一个我喜欢的引导模板。我在 python 中实现了一个小的 Flask 应用程序来充当服务器。

app.py

from flask import Flask, render_template
app = Flask(__name__)

@app.route('/')
def hello(name=None):
    return render_template('index.html', name=name)

if __name__ == '__main__':
    app.run()

这是 index.html 文件的主体

<body>
    <div class="main-row">
        <h1>Fortune Estates</h1>
        <div class="main_frame">
            <iframe class="frame-border scroll-pane" src="templates/main.html" frameborder="0"></iframe>
        </div>
    </div>
    <div class="copy-right">
        <p>&copy; 2016 Fortune Estates . All Rights Reserved | Design by <a href="http://w3layouts.com" target="_blank">W3layouts</a></p>
    </div>
    <script src="static/js/jquery.nicescroll.min.js"></script> 
    <script>
      $(document).ready(function() {

        var nice = $("html").niceScroll();  // The document page (body)

        $("#div1").html($("#div1").html()+' '+nice.version);

        $("#boxscroll").niceScroll(); // First scrollable DIV

      });
    </script>
</body>

当我启动烧瓶应用程序时,我收到此错误127.0.0.1 - - [14/Feb/2017 14:46:45] "GET /templates/main.html HTTP/1.1" 404 -

这是我的文件层次结构

.
+-- app.py
+-- templates 
|   +-- index.html
|   +-- main.html
|   ....
+-- static 
|   +--js
|   |   +--bootstrap.cs
|   |   ...
|   +--fonts
|   |   ...
|   +--css
|   |   ...
|   +--images
|   |

【问题讨论】:

    标签: python html twitter-bootstrap iframe flask


    【解决方案1】:

    您从 index.html 中引用 main.html 模板,它们位于同一目录中,请尝试在 iFrame 中使用 src="main.html"。

    然后浏览器将发送另一个对 main.html 文件的 GET 请求。您需要通过在 Flask 服务器中添加新功能来从服务器端覆盖它:

    from flask import Flask, render_template
    app = Flask(__name__)
    
    @app.route('/')
    def hello(name=None):
        return render_template('index.html', name=name)
    
    
    @app.route('/main.html')
    def main():
        return render_template('main.html')
    
    if __name__ == '__main__':
        app.run()
    

    【讨论】:

    • 仍然没有加载页面。 127.0.0.1 - - [14/Feb/2017 15:39:53] "GET /main.html HTTP/1.1" 404 -
    • 尝试使用编辑后的解决方案,它应该可以正常工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 2018-03-31
    • 2014-06-29
    相关资源
    最近更新 更多