如何将Python变量传递给html变量?

编程入门 行业动态 更新时间:2024-10-28 22:24:34
本文介绍了如何将Python变量传递给html变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要从python中的文本文件中读取一个url链接作为变量,并在html中使用它。 文本文件file.txt只包含一行 188.xxx。 xxx.xx:8878 ,这行应该保存在变量link中,然后我应该在html中使用这个变量的包含,这样当我点击按钮图像时应该打开链接go_online.png。我试图改变我的代码如下,但它不工作!请帮助吗?

I need to read a url link from text file in python as a variable, and use it in html. The text file "file.txt" contains only one line "188.xxx.xxx.xx:8878", this line should be saved in the variable "link", then I should use the contain of this variable in the html, so that the link should be opened when I click on the button image "go_online.png". I tried to change my code as following but it doesn't work! any help please?

#!/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)

推荐答案

first off ,不确定javascript部分有什么意义,只是将其忽略。此外,您打开 p 标记但不关闭它。不确定你的模板引擎是什么,但你可以传入纯Python中的变量。另外,请确保将链接放在引号中。所以你的代码应该是这样的:

first off, not sure that the javascript part makes any sense, just leave it out. Also, your opening a p tag but not closing it. Not sure what your templating engine is, but you could just pass in the variables in pure python. Also, make sure to put quotes around your link. So your code should be something like:

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是字符串占位符,它将在mult末尾包含%(firstString,secondString)中的变量我行字符串。

(btw, the %s things are string placeholders, that will be poplulated the variables in %(firstString, secondString) at the end of the the multi line string.

更多推荐

如何将Python变量传递给html变量?

本文发布于:2023-11-23 18:11:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1622407.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   如何将   Python   html

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!