python - 如何使用 html 和 cheeypy 将对象放在隐藏的输入字段中
问题描述
below is abc.html
<div xmlns:xi="http://www.w3.org/2001/XInclude"
xmlns:py="http://genshi.edgewall.org/" py:strip="True" >
<div class="insync-bluthm-tbl-wrp">
<div class="insync-bluthm-tbl-scroll">
<div class="insync-bluthm-tbl-scroll-inr">
<table class="insync-bluthm-tbl">
<thead>
<tr>
<th><div>File Name</div></th>
</tr>
</thead>
<tbody>
<input type="hidden" id="restorable_data" value="${restoreData}"/>
<tr>
<td><div>Dummy File name</div></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
below is python function
@cherrypy.expose
def can_restore_mds(self, *args, **kwargs):
restoreData = {
'abc': 'def',
'akjshd': 'asd',
'is_valid': 1,
}
restore_context = {
'page': 'abc.html',
'restoreData': restoreData,
}
html = render_page(restore_context, restore_context['page'])
return {
'html': html,
'restoreData': restoreData,
}
return response
“restoreData”是变量,在服务器端渲染中没有以正确的格式注入,任何人都可以帮助需要做什么吗?
解决方案
您可以使用 json 转储函数来执行此操作:-
Python
# remember to import json in your python file
restoreData = {
'abc': 'def',
'akjshd': 'asd',
'is_valid': 1,
}
restore_context = {
'page': 'abc.html',
'restoreData': json.dumps(restoreData),
}
HTML
<input type="hidden" id="restorable_data" value="${restoreData}"/>
推荐阅读
- swift - Swift 5. 'withUnsafeBytes' 已弃用:使用 `withUnsafeBytes
(...) 反而 - java - GeoFire Firebase 查询无法在 ArrayList 中存储用户 ID
- scala - Upickle:读取可能是 String 或 Int 的属性作为 String
- java - 将 Java 代码转换为 Kotlin 代码时出错
- ros - 将 C++ 包中的 ROS .msg 导入 python
- symfony - 重定向路由时如何传递隐藏参数?(与 POST ?)
- react-native - 是否有必要将 redux 表单字段包装在表单标签中?
- igraph - Shortest_paths 是否会考虑到同名顶点的所有路径,但索引 ID 不同?
- python - 如何从列表中提取值?
- r - 如何表示对数正态分布