一、核心
1、做按钮监听,打开窗口
function foo() { window.open('/add/', '', 'width=400,height=200,top=500, left=300') }
2、执行post请求后,返回一个额外的页面
def add_publish(request): if request.method == "POST": publisher = request.POST.get("publisher") print(publisher) # models.Publish.objects.create(name=publisher) return render(request, "pop.html", {"publish": publisher}) return render(request, "add_publish.html")
3、在这个页面添加 调用父窗口的函数,关闭窗口
<script> window.opener.bar("{{ publish }}"); window.close(); </script>
4、注意主页面的被调用的函数
function bar(data) { console.log(data); $(".my-bar").text(data)
pop.html相当于桥梁
本文介绍了使用JavaScript进行按钮监听并打开新窗口的方法,通过Python处理POST请求返回页面,以及如何在子窗口中调用父窗口函数实现数据传递与窗口关闭。涉及到的技术包括JavaScript的window.open方法、Python的Django框架请求处理及页面渲染。
8740

被折叠的 条评论
为什么被折叠?



