San*_*ram 2 src flask angularjs
我是新手,并使用它在"localhost:5000 /"服务index.html.目前我只有3个文件:index.html,angular.js和app.js; 并且所有这些都在同一个文件夹中.我正在为index.html服务:
@app.route('/')
def index():
return make_response(open('index.html').read())
Run Code Online (Sandbox Code Playgroud)
我想在index.html中包含angular.js和app.js. 我试过这个:
<script src="angular.js"></script>
<script src="app_controller.js"></script>
Run Code Online (Sandbox Code Playgroud)
但它不包括两个文件(我从"查看页面源"检查).是否有一个默认目录,其中flask包含文件?我该怎么做到这一点?
Flask应该知道它可以提供静态文件的路径.创建"静态"目录将所有静态文件放入其中.并且,在index.html中使用:
<script src="{{ url_for('static', filename='angular.js')}}"</script>
Run Code Online (Sandbox Code Playgroud)
请注意,有一个名为"static"的特殊端点,它会查看名为"static"的目录并提供该文件.您还可以更改静态目录的名称.
app = Flask(__name__, static_folder="differet_dir_name")
Run Code Online (Sandbox Code Playgroud)
此外,创建名为"templates"的目录并复制所有html文件,并在方法中使用render_template方法:
return render_template("index.html")
Run Code Online (Sandbox Code Playgroud)
因此,您的目录结构应如下所示:
app.py
static --> contains all static files
templates --> contains all html files
Run Code Online (Sandbox Code Playgroud)
链接:http ://flask.pocoo.org/docs/quickstart/#static-files http://flask.pocoo.org/docs/api/#application-object
希望能帮助到你.
| 归档时间: |
|
| 查看次数: |
1526 次 |
| 最近记录: |