解析从 python(tornado) 到 javascript 的路径变量
parsing path variables from python(tornado) to javascript
我在 python 中有一个代码,我试图将一些图像的动态路径发送到 javascript,但由于某种原因,'\'(分隔符)被删除了。我试图通过一次发送一张图像来实现这一目标。
我正在使用龙卷风作为 python 服务器并且有 javascript 代码 html.
server.py
import tornado.ioloop
import tornado.web
import time
import os
class one(tornado.web.RequestHandler):
def get(self):
self.write('Hello')
class Article(tornado.web.RequestHandler):
def get(self):
dir_path = os.path.dirname(os.path.realpath(__file__))
dir_path=os.path.join(dir_path, 'images')
file='2000px-Python-logo-notext.svg_.jpg'
file=os.path.join(dir_path, file)
var1 = 'Orignal_Appp'
self.render('template.html', var=var1, file1=file)
application = tornado.web.Application([
(r"/", one),
(r"/articles", Article),
],debug=True)
if __name__ == "__main__":
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()
template.html
<html>
<title>
</title>
<body>
<br>
<h1 align="center"> </h1>
<br>
<div id='div2' align="center">
<h4>{{var}}</h4>
</div>
<div id='div1' align="center" >
<p><img id="dynamicImg" src="" style="width:300px;height:300px;"/></p>
</div>
</body>
<script>
var N = "{{ file1 }}";
var img = document.getElementById('dynamicImg');
img.src = N
document.write(N)
</script>
</html>
图像路径的输出来自"document.write(N)":
D:namanimagesimages0px-Python-logo-notext.svg_.jpg
应该是这样的:
D:\naman\images\images00px-Python-logo-notext.svg_.jpg
此外,如果在 server.py 中我将 'images/2000px-Python-logo-notext.svg_.jpg' 作为硬编码路径值而不是变量传递,它工作正常。所以,我的理解是 javascript 正在解析路径并删除 '\' 反斜杠。我很确定,这是因为反斜杠字符,我认为这不会发生在烧瓶中。所以它可能与龙卷风有关,虽然我不确定。这还不如有一个非常common/general的解决方案。让我知道。
硬编码:
self.render('template.html', var=var1, file1='images/2000px-Python-logo-notext.svg_.jpg')
变量:
self.render('template.html', var=var1, file1=file)
我在 windows 10,Python 3.7.3,tornado==5.1.1,但我也想了解其他平台(linux) .
另外,这可能是我可能忽略的一个非常普遍的问题。我不确定。
我四处寻找这种情况以及其他情况,但可能这个问题是重复的,在这种情况下,请指导我解决问题。如果有人知道任何 javascript 函数来解决这个问题
任何信息,帮助将不胜感激。
提前致谢!
var N = "{{ file1 }}";
变成 var N = "D:\naman\images\images00px-Python-logo-notext.svg_.jpg";
,backslash is a special character 变成 javascript 字符串。
您需要转义 该字符串,以便在 javascript 字符串文字中使用。 Tornado 的默认转义行为适用于 HTML(其中像尖括号这样的字符是特殊的,但反斜杠不是)。最简单的方法是使用 json_encode
函数(它将 python 字符串转换为 javascript 字符串):
var N = {% raw json_encode(file1) %};
请注意,此处没有引号 - json_encode
负责添加引号。
转义很棘手 - 您应该确保使用包含反斜杠、尖括号、空格、& 符号和加号等特殊字符的路径测试您的应用程序。
我在 python 中有一个代码,我试图将一些图像的动态路径发送到 javascript,但由于某种原因,'\'(分隔符)被删除了。我试图通过一次发送一张图像来实现这一目标。 我正在使用龙卷风作为 python 服务器并且有 javascript 代码 html.
server.py
import tornado.ioloop
import tornado.web
import time
import os
class one(tornado.web.RequestHandler):
def get(self):
self.write('Hello')
class Article(tornado.web.RequestHandler):
def get(self):
dir_path = os.path.dirname(os.path.realpath(__file__))
dir_path=os.path.join(dir_path, 'images')
file='2000px-Python-logo-notext.svg_.jpg'
file=os.path.join(dir_path, file)
var1 = 'Orignal_Appp'
self.render('template.html', var=var1, file1=file)
application = tornado.web.Application([
(r"/", one),
(r"/articles", Article),
],debug=True)
if __name__ == "__main__":
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()
template.html
<html>
<title>
</title>
<body>
<br>
<h1 align="center"> </h1>
<br>
<div id='div2' align="center">
<h4>{{var}}</h4>
</div>
<div id='div1' align="center" >
<p><img id="dynamicImg" src="" style="width:300px;height:300px;"/></p>
</div>
</body>
<script>
var N = "{{ file1 }}";
var img = document.getElementById('dynamicImg');
img.src = N
document.write(N)
</script>
</html>
图像路径的输出来自"document.write(N)":
D:namanimagesimages0px-Python-logo-notext.svg_.jpg
应该是这样的:
D:\naman\images\images00px-Python-logo-notext.svg_.jpg
此外,如果在 server.py 中我将 'images/2000px-Python-logo-notext.svg_.jpg' 作为硬编码路径值而不是变量传递,它工作正常。所以,我的理解是 javascript 正在解析路径并删除 '\' 反斜杠。我很确定,这是因为反斜杠字符,我认为这不会发生在烧瓶中。所以它可能与龙卷风有关,虽然我不确定。这还不如有一个非常common/general的解决方案。让我知道。
硬编码:
self.render('template.html', var=var1, file1='images/2000px-Python-logo-notext.svg_.jpg')
变量:
self.render('template.html', var=var1, file1=file)
我在 windows 10,Python 3.7.3,tornado==5.1.1,但我也想了解其他平台(linux) . 另外,这可能是我可能忽略的一个非常普遍的问题。我不确定。 我四处寻找这种情况以及其他情况,但可能这个问题是重复的,在这种情况下,请指导我解决问题。如果有人知道任何 javascript 函数来解决这个问题 任何信息,帮助将不胜感激。
提前致谢!
var N = "{{ file1 }}";
变成 var N = "D:\naman\images\images00px-Python-logo-notext.svg_.jpg";
,backslash is a special character 变成 javascript 字符串。
您需要转义 该字符串,以便在 javascript 字符串文字中使用。 Tornado 的默认转义行为适用于 HTML(其中像尖括号这样的字符是特殊的,但反斜杠不是)。最简单的方法是使用 json_encode
函数(它将 python 字符串转换为 javascript 字符串):
var N = {% raw json_encode(file1) %};
请注意,此处没有引号 - json_encode
负责添加引号。
转义很棘手 - 您应该确保使用包含反斜杠、尖括号、空格、& 符号和加号等特殊字符的路径测试您的应用程序。