javascript - 在 ubuntu 中使用 Flask 将 .js 包含在 .html 中时出现引用错误

标签 javascript python html flask

我正在尝试在 nev.html 中包含 str.jsjit.js 文件。两者都在同一目录中,但它给了我一个引用错误:

flask文件位于flask/app1.py

import json
import simplejson
from flask import Flask, render_template, request
app = Flask(__name__)

@app.route('/')
def hello():
    return 'client.html'

@app.route('/returnjson')
def returnjson():
    str1 = json.dumps({"name":"Infinetly Beta","friends":[{"name":"Aamir","friends":[{"name":"Jesal","friends":[]}]},{"name":"Neville","friends":[{"name":"Kapil","friends":[]}]}]})
    return str1

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

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

当我使用 Flask 运行它时,出现以下错误:

neville@neville-laptop:~/Flask$ python app1.py
 * Running on http://127.0.0.1:5000/
 * Restarting with reloader
127.0.0.1 - - [30/Jan/2012 02:01:53] "GET /neville HTTP/1.1" 200 -
127.0.0.1 - - [30/Jan/2012 02:01:53] "GET /jit.js HTTP/1.1" 404 -
127.0.0.1 - - [30/Jan/2012 02:01:53] "GET /css/base.css HTTP/1.1" 404 -
127.0.0.1 - - [30/Jan/2012 02:01:53] "GET /css/RGraph.css HTTP/1.1" 404 -
127.0.0.1 - - [30/Jan/2012 02:01:53] "GET /str.js HTTP/1.1" 404 -
127.0.0.1 - - [30/Jan/2012 02:01:53] "GET /favicon.ico HTTP/1.1" 404 -
127.0.0.1 - - [30/Jan/2012 02:01:54] "GET /returnjson HTTP/1.1" 200 -

目录结构/flask/templates

neville@neville-laptop:~/Flask$ ls -lc templates
total 912
-rw-r--r-- 1 neville neville    533 2012-01-29 04:52 client_(copy).html
-rw-r--r-- 1 neville neville    586 2012-01-28 05:01 client.html
drwxr-xr-x 2 neville neville   4096 2012-01-29 05:44 css
-rwxr-xr-x 1 neville neville   1628 2012-01-30 01:47 example1.html
-rw-r--r-- 1 neville neville    677 2012-01-27 14:03 ify.html
-rwxr-xr-x 1 neville neville 479578 2012-01-27 14:43 jit.js
-rwxr-xr-x 1 neville neville 158518 2012-01-27 14:43 jit-yc.js
-rw-r--r-- 1 neville neville 248235 2012-01-29 04:50 jquery.js
-rw-r--r-- 1 neville neville   1165 2012-01-30 02:01 nev.html
-rwxr-xr-x 1 neville neville   6963 2012-01-30 01:08 stripped.js
-rwxrwxrwx 1 neville neville   5767 2012-01-30 01:40 str.js

目录结构/flask

neville@neville-laptop:~/Flask$ ls -lc 
total 744
-rw-r--r-- 1 neville neville    524 2012-01-30 01:51 app1.py
-rw-r--r-- 1 neville neville    540 2012-01-28 01:27 app2.py
drwxr-xr-x 5 neville neville   4096 2012-01-27 07:19 env
-rw-r--r-- 1 neville neville    432 2012-01-28 04:58 hello.py
-rwxr-xr-x 1 neville neville 479578 2012-01-29 08:49 jit.js
-rw-r--r-- 1 neville neville 248235 2012-01-29 04:50 jquery.js
-rwxrwxrwx 1 neville neville   5767 2012-01-30 01:57 str.js
drwxr-xr-x 3 neville neville   4096 2012-01-30 02:01 templates

str.js 文件

function init(data){  
   document.getElementById('container').innerHTML = data.name;
}  
    

nev.html

<html>
<head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <!-- JIT Library File -->
    <script language="javascript" type="text/javascript" src="jit.js"></script>

    <!-- CSS Files -->
    <link type="text/css" href="/css/base.css" rel="stylesheet" />
    <link type="text/css" href="/css/RGraph.css" rel="stylesheet" />

    <title>WikiG</title>

    <!-- Example File -->
    <script language="JavaScript" type="text/JavaScript" src="str.js"></script>
    
            <script type="text/javascript">
            $(document).ready(function(){
            $("button").click(function(){
                $.getJSON("/returnjson",
                function(data){
                    init(data);
                });//getjson
            });//click
        });//ready
    
    </script>
    
</head>

<body>
    
    <button>Click me</button>
    <div id="container">
</body>
</html>

最佳答案

将 css 和 js 保存在 Flask 应用程序根目录中“static”文件夹下的文件夹中。类似以下层次结构的内容会有所帮助:

Flask/static/javascripts/*.js
Flask/static/stylesheets/*.css

并更改模板和其他文件中的引用。

关于javascript - 在 ubuntu 中使用 Flask 将 .js 包含在 .html 中时出现引用错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9060654/

相关文章:

javascript - 当 cdn js cloudflare 不可用时 lightbox2 回退

javascript - 杂乱的 JavaScript 标签,如何改进 index.html

javascript - Reactjs 状态未通过 redux 更新

Python:获取所有节点的度数,然后在networkx中绘制boxplot

php更新mysql

javascript - React 组件 css 设置不正确

python - XPath 内置函数及其与 lxml 的使用

python - django python shell数据访问错误

javascript - 避免与固定元素重叠

html - 将 html 类结合到 css 中以便于设计?