如何将python变量传递给html变量?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了如何将python变量传递给html变量?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1962字,纯文字阅读大概需要3分钟。
内容图文
![如何将python变量传递给html变量?](/upload/InfoBanner/zyjiaocheng/793/acf540ebf0f049bb9c4a5bb11aa655de.jpg)
我需要从python中的文本文件中读取一个url链接作为变量,并在html中使用它.
文本文件“file.txt”只包含一行“http://188.xxx.xxx.xx:8878”,这行应该保存在变量“link”中,然后我应该在html中使用这个变量的包含,这样当我打开链接时点击按钮图片“go_online.png”.我试着改变我的代码如下,但它不起作用!有什么帮助吗?
#!/usr/bin/python
import cherrypy
import os.path
from auth import AuthController, require, member_of, name_is
class Server(object):
_cp_config = {
'tools.sessions.on': True,
'tools.auth.on': True
}
auth = AuthController()
@cherrypy.expose
@require()
def index(self):
f = open ("file.txt","r")
link = f.read()
print link
f.close()
html = """
<html>
<script language="javascript" type="text/javascript">
var var_link = '{{ link }}';
</script>
<body>
<p>{htmlText}
<p>
<a href={{ var_link }} ><img src="images/go_online.png"></a>
</body>
</html>
"""
myText = ''
myText = "Hellow World"
return html.format(htmlText=myText)
index.exposed = True
#configuration
conf = {
'global' : {
'server.socket_host': '0.0.0.0', #0.0.0.0 or specific IP
'server.socket_port': 8085 #server port
},
'/images': { #images served as static files
'tools.staticdir.on': True,
'tools.staticdir.dir': os.path.abspath('/home/ubuntu/webserver/images')
}
}
cherrypy.quickstart(Server(), config=conf)
解决方法:
首先,不确定javascript部分是否有意义,只是把它留下来.此外,您打开一个标签,但没有关闭它.不确定你的模板引擎是什么,但你可以只传入纯python中的变量.另外,请务必在链接周围加上引号.所以你的代码应该是这样的:
class Server(object):
_cp_config = {
'tools.sessions.on': True,
'tools.auth.on': True
}
auth = AuthController()
@cherrypy.expose
@require()
def index(self):
f = open ("file.txt","r")
link = f.read()
f.close()
myText = "Hello World"
html = """
<html>
<body>
<p>%s</p>
<a href="%s" ><img src="images/go_online.png"></a>
</body>
</html>
""" %(myText, link)
return html
index.exposed = True
(顺便说一句,%s的东西是字符串占位符,它将在多行字符串末尾的%(firstString,secondString)中填充变量.
内容总结
以上是互联网集市为您收集整理的如何将python变量传递给html变量?全部内容,希望文章能够帮你解决如何将python变量传递给html变量?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。