包括外部脚本在web.py(html模板)中不起作用

时间:2017-03-13 03:55:30

标签: javascript html web.py

我在html(web模板)中包含脚本,当我通过http://127.0.0.1:8080/main访问它时它不起作用,但在本地,当我双击html文件时,脚本可以工作。

我在这里找到一个问题,但答案也不起作用: including script doesn't work in web.py

# web.py
import web

urls = ('/main', 'main')
render = web.template.render('templates/')


class main:
    def GET(self):
        return render.main()


app = web.application(urls, globals())

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

下面是HTML

<!DOCTYPE html>

<html>
  <head>
    <title>title</title>
    <script src="echarts.min.js"></script>
  </head>
  <body>
    <p>Hello world!+++</p>

1 个答案:

答案 0 :(得分:0)

您的目录结构是如何定义的?您在 static 文件夹中有js个文件吗?

要使cssjs等任何静态脚本生效,web.py需要它们位于主app文件夹下的静态文件夹中。 这是我通常使用的一般结构。

/app.py           #main app 

/static           #static data
       /css       #css
       /js        #javascript
       /images    #images

/templates        #html templates

可以使用html文件中的相对路径访问这些文件夹 对于例如一旦定义了上述目录结构,只需在html文件中输入

即可
<script src="static/js/yourJavaScript.js" ></script>

当你启动app.py时,应用程序会查看你的javascript的static / js文件夹,它应该可以工作。
我的设置如上所述。

相关问题