python - Flask's url_for function does not get interpolated within html code
问题描述
@app.route( '/log=<page>' )
def log( page ):
....
....
# add counter to html template and start rendering
counter = '''<center>
<table bgcolor=black bordercolor=orangered>
<td><font size=3 color=lime> Αριθμός Επισκεπτών </font></td>
<td><a href="http://superhost.gr/log=%s"> <font size=3 color=plum> %d </font></a></td>
</table>
''' % (page, pagehit)
Why the above code cannot be written as follows. It doesn't get interpolated.
# add counter to html template and start rendering
counter = '''<center>
<table bgcolor=black bordercolor=orangered>
<td><font size=3 color=lime> Αριθμός Επισκεπτών </font></td>
<td><a href="<a href="{{ url_for('/log', page='%s' }}">"> <font size=3 color=plum> %d </font></a></td>
</table>
''' % (page, pagehit)
Also when i try an inline statement to avoid for block i get an error. It should have worked.
name = nikos if (5>3)
解决方案
You can either pass the variable to the template and have the {{url_for inside your template, then the rendering engine will take care of that IIRC (RECOMMENDED)
Or you can evaluate it like:
counter = '''<center>
<table bgcolor=black bordercolor=orangered>
<td><font size=3 color=lime> Αριθμός Επισκεπτών </font></td>
<td><a href="{url}"> <font size=3 color=plum> {number} </font></a></td>
</table>
'''.format(url={{ url_for('/log', page=page) }}, number=pagehit)
Also you have a bug in the second code you post it says
<a href="<a href="{{ url_for('/log', page='%s' }}">">
推荐阅读
- android - 如何在标签布局中实现插页式广告?
- react-native - 如何将 this.props 数据放入 headerRight 选择器?
- python - python如何使用for循环打印出字符串中的元音
- javascript - 在 Document Write 上应用功能并在 textarea 上显示
- javascript - 动态 JS 频道菜单链接问题
- python - 使用列表推导代替两个 for 循环
- unity3d - AudioSource.Play() 在枚举器中不起作用
- scala - 如何使用 Spark 递归聚合树状(分层)数据?
- python - Python - 输入用户输入的天数,然后显示等效的年、月和日
- bash - 如果 xargs 没有参数,请提前退出管道