Flask:在模板中放置静态javascript文件的位置 [英] Flask: where to put static javascript files in templates

查看:83
本文介绍了Flask:在模板中放置静态javascript文件的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发具有以下文件夹结构的flask应用程序:

I'm developing a flask app with the following folder structure:

|-->flask_app.py
    |-->static
        |-->css
            |-->bootstrap.min.css
            |-->styles.css
        |-->js
            |-->jquery-3.1.1.min.js
            |-->bootstrap.min.js
            |-->script.js
    |-->templates
        |-->index.html

链接到 index.html 中的这些CSS和js文件的正确方法是什么,我需要与它们关联哪些参数?

What is the proper way to link to these css and js files in index.html and what parameters do I need associated with them?

我的CSS链接如下所示,并位于标题中:

My CSS links look like this and are located in the header:

<link rel="stylesheet" href="{{ url_for('static', filename='css/styles.css') }}">

我的JS链接看起来像这样,位于body标记的末尾:

And my JS links look like this and are located at the end of the body tag:

<script src="{{ url_for('static', filename='js/script.js') }}"></script>

这是正确的语法吗?它们是否位于我模板中的正确位置(我确定这里有灵活性)?

Is this the correct syntax? Are they located in the correct spots in my templates (I'm sure there's flexibility here)? And are there any other parameters I should pass in (e.g. type="text/css", type="text/javascript", media="screen")?

一切正常,但是如果有的话,我想遵循建议的做法.

Everything is working as expected but I want to follow recommended practice if there is any.

推荐答案

作为

As the Flask documentation mentions, you should store .css and .js files within your static folder and for organizational purposes, its fine to have each type of file as subdirectories (especially as your app grows).

每个此SO答案,您不需要在Jinja表达式中输入type ="text/css"或type ="text/javascript".

Per this SO answer, you don't need to have type="text/css" or type="text/javascript" in the jinja expression.

这篇关于Flask:在模板中放置静态javascript文件的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆