gpt4 book ai didi

javascript - 从 Django 的 View 中删除查询字符串

转载 作者:行者123 更新时间:2023-11-30 19:24:05 25 4
gpt4 key购买 nike

我有一个名为控制面板的页面,我可以在其中启动/停止脚本。当按下启动和停止脚本的按钮时,它返回相同的页面,按钮颜色已更改,我想删除查询字符串。

view.py

def controlpanel(request):
data = {'paypal': paypal_, 'cpu': cpu_usage, 'active': active_task ...}
return render(request, 'admin/controlpanel.html', context=data)

def startapp(request):
if request.META['QUERY_STRING']:
#... start scripts etc..
request.META['QUERY_STRING'] = False

return controlpanel(request)

函数返回带有查询字符串的控制面板...(127.0.0.1:8000/startapp?run=True 但我只想要 127.0.0.1:8000/controlpanel)

controlpanel.html

<div>
{% if active %}
<button onclick="f()" class="btn btn-md btn-success">Start</button>
{% else %}
<button onclick="f()" class="btn btn-md btn-warning">Start</button>
{% endif %}
</div>


<script>
function f() {
let a = null;
if ('{{active}}' === 'True') {
a = 'stop'
} else {
a = 'start'
}
window.location.href = "/startapp?run=" + a;
}
</script>

最佳答案

你可以使用 Django 的 redirect返回不带查询参数的 URL。

关于javascript - 从 Django 的 View 中删除查询字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57107018/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com