Reputation: 3587
I have a very simple query and I apologies, but I have been stuck without being able find the answer for a while.
In the script bellow I am trying to "dynamically" create a url using some variable url1 and url2 generated by python further down.
I have tried with % but no success either.
Could someone indicate what is wrong with the code.
Thanks
import cherrypy
class PageGenerator(object):
page_template = """<html>
<head></head>
<body>
<div><p><object data=http://en.wikipedia.org/w/index.php?title={{url1}} width="1200" height="600"> Error: Embedded data could not be displayed. </object>
</p>
<p><object data=http://en.wikipedia.org/w/index.php?title={{url2}} width="1200" height="600"> Error: Embedded data could not be displayed. </object>
</p></div>
</body>
</html>"""
@cherrypy.expose
def generate(self, url1):
url1 = "Neurosurgery "
return url1
@cherrypy.expose
def generate(self, url2):
url2 = "Urology "
return url12
if __name__ == '__main__':
cherrypy.quickstart(PageGenerator())
Upvotes: 0
Views: 764
Reputation: 25234
Python has 3 standard ways of string formatting (or interpolation).
%
(modulo operator). 'one %s, two %s' % (var1, var2)
.str.format
. 'one {0}, two {1}'.format(var1, var2)
. from string import Template
t = Template('one $var1, two $var2')
t.substitute(var1 = var1, var2 = var2)
The placeholder syntax you use in the template is not implemented in Python's batteries. Besides by returning a string, (url1
and url2
in your case), from a page handler, CherryPy will obviously just display it. It knows nothing about your page_template
attribute, and you need to instruct CherryPy to use it.
First, decide on template syntax. Either use standard 3, or learn about Jinja2 (the syntax you're using). In latter case, StackOverflow has plenty of information. Second, do interpolation not just return a variable.
Upvotes: 1