popup
from django.shortcuts import render,HttpResponse from django.urls import reverse # Create your views here. from app01 import models def test(request): user_group_list = models.UserGroup.objects.all() return render(request,'test.html',{'user_group_list':user_group_list}) def add_test(request): if request.method == "GET": return render(request,'add_test.html') else: popid = request.GET.get('popup') if popid: # 通过popup新创建了一个页面进来 title = request.POST.get('title') obj = models.UserGroup.objects.create(title=title) # response = {'id':obj.id,'title': obj.title} # 1. 关闭popup页面 # 2. 将新增的数据添加,传送到原来发送pop页面中的ugID标签位置 popid = ugID return render(request,'popup_response.html',{'id':obj.id,'title':obj.title,'popid':popid }) else: title = request.POST.get('title') models.UserGroup.objects.create(title=title) return HttpResponse('重定向列表页面:所有用户组')
from django.conf.urls import url,include from django.contrib import admin from app01 import views from yingun.service import v1 urlpatterns = [ url(r'^test/',views.test), url(r'^add_test/',views.add_test), ]
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <p>用户名: <input type="text"></p> <p>用户组: <select id="ugID"> {% for row in user_group_list %} <option value="{{ row.pk }}">{{ row.title }}</option> {% endfor %} </select> <a href="#" onclick="popupUrl('/add_test/?popup=ugID')">增加</a> </p> <script> function popupUrl(url) { window.open(url,"xxxxxx","status=1,height:500,width:600,toolbar=0,resizeable=0"); } function popupCallBackl(popid,id,title) { var tag = document.createElement("option"); tag.innerText = title; tag.setAttribute("value",id); tag.setAttribute("selected","selected"); var selectid = document.getElementById(popid); selectid.appendChild(tag); } </script> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>增加测试数据</h1> <form action="" method="post"> {% csrf_token %} <input type="text" name="title"> <input type="submit" value="提交"> </form> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>正在关闭的pop页面</title> </head> <body> <script> //将数据创送给发起popup的页面 opener.popupCallBackl('{{ popid }}','{{ id }}','{{ title }}'); window.close(); </script> </body> </html>