How to pass python variable to html variable?

后端 未结 1 519
孤城傲影
孤城傲影 2021-01-06 04:41

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

相关标签:
1条回答
  • 2021-01-06 05:16

    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
    

    (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.

    0 讨论(0)
提交回复
热议问题