popup 示例
1.目录结构
2.views.py
from django.shortcuts import render def index(request): return render(request,'index.html') def pop(request): if request.method == "GET": return render(request, 'pop.html') else: user = request.POST.get('user') return render(request,'pop_response.html',{'user':user})
3.urls.py
from django.conf.urls import url from django.contrib import admin from app01 import views urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^index/', views.index), url(r'^pop/', views.pop), ]
4.tempaltes
indel.html:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1 id="i1">无所谓</h1> <a href="#" onclick="popUp('http://www.oldboyedu.com')">点我点我</a> <script> function xxxpopupCallback(text) { document.getElementById('i1').innerHTML = text; } function popUp(url) { window.open( '/pop/', 'n1' ,"status=1, height:500, 600, toolbar=0, resizeable=0"); } </script> </body> </html>
pop.html:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form method="post"> {% csrf_token %} <input type="text" name="user"> <input type="submit" value="保存"> </form> </body> </html>
pop_response:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>正在关闭</h1> <script> (function () { // 可以调用popup原页面的一个函数 opener.xxxpopupCallback("{{ user }}"); window.close(); })() </script> </body> </html>
分析:
技术点:自执行函数
(function () {
})
实时更新:
解决方法:3种方式
1.popup 涉及 js 语法。 补充到 组件 里。 -问题:点击,弹出新窗口,保存完事,页面不刷新数据返回。怎么解决???? -实时更新 class UserForm(Form): part = fields.ChoiceFiedld(choices=modesls.Department.object.filter.values_list('id','name') 解决方式1:super方法重构方法 class UserForm(Form): part = fields.ChoiceField(choices=models.Department.object.values_lists('id','name')) def __init__(self,*args,**kwargs): super(UserForm,self).__init__(*args,**kwargs) self.fields['part'].choices = models.Department,objects.values_list('id','name') 解决方式2:利用modelChoiceField方法 class UserForm(Form): part = fields.ModelChoiceField(queryset=models.Department.obejct.all()) part = fields.ModelMultipleChoiceField(queryset=models.Department.obejct.all()) 方式3:ModelForm class UserForm(ModelForm): class Meta: model = UserInfo field = "__all__" 判断: model 中的字段 如果是 FK: ModelChoiceField 如果是 M2M: ModelMultipleChoiceField