本文介绍了如何将Python变量传递给html变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从python中的文本文件中读取一个url链接作为变量,并在html中使用它。
文本文件file.txt只包含一行,这行应该保存在变量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 "http://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变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 22:55