我有一个 Django 模板,我可以通过单击按钮来调用视图。view 函数返回 aHttpResponseRedirect到同一页面。我希望页面返回/刷新到与用户单击按钮时相同的滚动位置。
到目前为止,这是我尝试过的:
尝试1:基于this和this,我onsubmit在表单中添加了一个,如下所示:
<form action="{% url 'buy' market.id %}" method="post" onsubmit="return refreshToSameScroll()">
Run Code Online (Sandbox Code Playgroud)
...location.reload()通过以下脚本调用:
<script type="text/javascript">
function refreshToSameScroll() {
location.reload();
}
</script>
Run Code Online (Sandbox Code Playgroud)
但这没有用。
尝试 2:然后我尝试了以下组合,并从此进行了修改:
<form action="{% url 'buy' market.id %}" method="post" onsubmit="return saveScroll()">
Run Code Online (Sandbox Code Playgroud)
...使用这个脚本:
<script type="text/javascript">
function saveScroll () {
var page_y = document.getElementsByTagName("body")[0].scrollTop;
window.location.href = window.location.href.split('?')[0] + '?page_y=' + page_y;
}
window.onload = function () {
if ( window.location.href.indexOf('page_y') != …Run Code Online (Sandbox Code Playgroud) 我有一个JSON格式的API response,当我使用时将其转换为字典时,格式如下json.loads(response):
{
"stories" : [
{
"headline" : "some text",
"summary" : [
"some more text"
],
"texts" : [
{
"title" : "even more text",
"id" : "a number"
},
{
"title" : "more text",
"id" : "another number"
}
]
}
]
}
Run Code Online (Sandbox Code Playgroud)
让我们称这本字典dict。
我可以访问stories使用dict['stories']。但是我该如何使用summary钻头?
我以为我可以做,dict['stories']['summary']但这会引发错误list indices must be integers or slices, not str。
我有一个可能如下所示的 python 列表:
['22', '0', '0', '0', '1, 0, 0, 0, 0']
Run Code Online (Sandbox Code Playgroud)
我希望它看起来像这样:
[22, 0, 0, 0, 1, 0, 0, 0, 0]
Run Code Online (Sandbox Code Playgroud)
由于最后一个元素不是整数,我不能map像这里建议的那样使用。并ast按照此处的建议使用,并没有让我一路走好:
[22, 0, 0, 0, (1, 0, 0, 0, 0)]
Run Code Online (Sandbox Code Playgroud)
任何建议将不胜感激。