2017-06-17 186 views
0

新来烧瓶,并有一些python的经验,当使用render_template时,它不会呈现模板,也不会给出任何错误。 代码这里:烧瓶render_template

from flask import Flask, render_template 

app = Flask(__name__, template_folder= "/templates") 




@app.route("/") 
def index(): 
     #return("Index str") 
    return render_template("index.html") 


@app.route("/crawler") 
def crawler(): 
    return("WebCrawler str") 
    return render_template("crawler.html") 

if __name__ == "__main__": 
    app.run() 
    app.debug = True 

HTML这里(很肯定的文件层次是正确的)。

<!DOCTYPE html> 

<html lang="en"> 
    <head> 
     <link rel="stylesheet" type="text/css" href="{{url_for('static', filename='style.css')}}"> 
     <meta charset = "utf-8"> 
     <title> Index</title> 
    <head> 
    <body> 
     <p> This webserver hosts all webapps and will showcase any unfinished applications. 
      </p> 

     </body> 

</html> 

root virtual environment

static for css

templates folder

回答

2

为HTML文件的默认文件夹是模板。因此,在您的python文件所在的位置创建一个名为“模板”的文件夹。将任何HTML文件放在该文件夹中

+0

最后一个屏幕截图显示了main.py所在的文件夹中的模板文件夹。 – Hobnob

+0

你是否试图通过本地主机访问该网站?如果是这样,那么你需要导入操作系统并具有以下代码:'if __name__ =='__main__': port = int(os.environ.get('PORT',5000)) app.run(host ='0.0。 0.0',port = port)' –

+0

事实上,我从来没有想过,我只是现在就测试一下。 – Hobnob

0

默认瓶template_folder设置为templates。如果你想改变它,然后在应用程序的根路径中提到文件夹。

It't时不必templates文件夹,但它应该是...

app = Flask(__name__, template_folder= "templates") 
+0

我已经尝试将其保留为默认值,但这也不起作用,如果需要,我可以显示归档的屏幕截图吗? – Hobnob

+0

@Hobnob那会很好。请更新您的问题与提交截图... –

+0

你可以把'app.debug = True'在'app.run()'语句之上。它会给你更好的错误信息... –