为什么我的烧瓶应用程序返回空页面

时间:2012-08-31 01:27:40

标签: python flask

我是Python Web框架的新手并且正在尝试学习Flask。我经历了教程,一切都很好。我现在正在尝试创建自己的小应用程序来学习框架。我在我的main flask.py文件中有以下代码

@app.route('/') 
def index():
    return render_template('main.html')

在main.html中我有这个HTML

{% extends "layout.html" %}
{% block content %}
<div> Foo bar test</div>
{% endblock %}

然后在layout.html中我有一个基本的Web布局,看起来像这样

<!DOCTYPE html>       
<title>Flaskpad</title>     
<link href="/static/css/bootstrap.css" rel="stylesheet">    
<link href="/static/css/flaskpad.css" rel="stylesheet">
<style type="text/css">  
.socials {   
padding: 10px;   
}   
</style>  

</head>  
<body>  
    <div class="navbar navbar-fixed-top">  
        <div class="navbar-inner">  
                <div class="container">  
                    <ul class="nav">  
                    <li class="active">  
                            <a class="brand" href="#">Flaskpad/a>  
                    </li>  
                    <li><a href="#">Login</a></li>  
                    <li><a href="#">About</a></li>  
                    <li><a href="#">Contact</a></li>  
                </ul> 
            </div>
        </div>
    </div>
    <div class="container">
        <div class="row fcontent">
            <div class="span3">
                Empty Space
            </div>
            <div class="span6 maincontent">
                {% block content %}{% endblock %}
            </div>
            <div class="span3">
                Empty Space
            </div>
        </div>
        <div class="row ffooter">
            <div class="span12">
                Made by Bar Foo
            </div>
        </div>
    </div> 
</body>  

我知道css链接输入不正确但我只是将它们作为占位符。现在,当我运行python flask.py并转到localhost:5000时,页面变为空白,我无法弄清楚原因。如果我在扩展之前在main.html中输入正常文本,那么我知道它正在加载main.html但它似乎没有扩展布局。页面字面上是空白的,因为当您查看源时什么也没有。我无法弄清楚这一点。

5 个答案:

答案 0 :(得分:3)

适合我。您是否将HTML文件放在相对于python模块名为“templates”的文件夹中?

这是我的完整测试代码。

from flask import Flask, render_template

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('main.html')

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

编辑:你说:“我在我的main flask.py文件中有以下代码” - 你可能不应该将它命名为flask.py,这可能会搞砸了。

答案 1 :(得分:0)

您使用哪种网络浏览器进行测试?

我尝试了你的代码,它使用chrome和firefox为我工作,但我确实注意到你的layout.html行中有一个错误关闭的锚标记

<a class="brand" href="#">Flaskpad/a>

也许这会导致浏览器将页面显示为空白?

答案 2 :(得分:0)

您是否错过了layout.html中的开始<head>标记?我什么都看不到。

答案 3 :(得分:0)

我放弃了,我无法让它发挥作用。我创建了一个新目录,将文本复制并粘贴到新文件中,然后运行它并成功运行。

我不知道为什么在这种情况下这不起作用。

答案 4 :(得分:0)

HTML文件必须位于templates文件夹中。也许那就是问题?此外,@app.route的每个函数都必须return render_template("the html file to render",**the_data_to_fill_the_html_file_with),其中the_data_to_fill_the_html_file_with是这样的:{'a_template_variable':the_value_to_fill_it_with}

相关问题