【问题标题】:including external script doen't work in web.py (html template)包括外部脚本在 web.py(html 模板)中不起作用
【发布时间】:2017-08-03 01:01:34
【问题描述】:

我在html中包含脚本(网页模板),当我通过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>

【问题讨论】:

  • "Doesn't work" 意思是……找不到js文件,或者文件加载了但没有执行,或者/main什么都没有返回……?

标签: javascript html web.py


【解决方案1】:

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

要使cssjs 等任何静态脚本正常工作,web.py 期望它们位于主应用程序文件夹下的静态文件夹中。 这是我通常使用的一般结构。

/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 文件夹,它应该可以工作。
我的设置如上所述。

【讨论】:

    猜你喜欢
    • 2011-09-11
    • 2018-07-18
    • 1970-01-01
    • 1970-01-01
    • 2021-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多