jinja2 template not found and internal server error

守給你的承諾、 提交于 2019-12-12 14:34:11

问题


Python code:

from flask import Flask, render_template

app = Flask(__name__)

@app.route("/")

def hello():

    return render_template('testing.html')

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

Html file:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>My name is pk</h1>
</body>
</html>

Also how to enable jinja2 in pycharm community version. I am directly creating html file and .py file in the same project


回答1:


flask file structure

|-app
|--templates // where your html files must be in
|--static // where your js and css files must be in
|--.py files
|--other packages

Also jinja is enabled in your system, if you have already downloaded flask package.




回答2:


By default, Flask looks in the templates folder in the root level of your app.

http://flask.pocoo.org/docs/0.10/api/

template_folder – the folder that contains the templates that should be used by the application. Defaults to 'templates' folder in the root path of the application.

So you have some options,

  1. rename template to templates
  2. supply a template_folder param to have your template folder recognised by the flask app:

    app = Flask(__name__, template_folder='template')
    

Flask expects the templates directory to be in the same folder as the module in which it is created; You'll need to tell Flask to look elsewhere instead:

app = Flask(__name__, template_folder='../pages/templates')

This works as the path is resolved relative to the current module path.

You cannot have per-module template directories, not without using blueprints. A common pattern is to use subdirectories of the templates folder instead to partition your templates. You'd use templates/pages/index.html, loaded with render_template('pages/index.html'), etc.



来源:https://stackoverflow.com/questions/42465768/jinja2-template-not-found-and-internal-server-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!