python函数和⽅法的区别
⼀、函数和⽅法的区别
1、函数要⼿动传self,⽅法不⽤传
2、如果是⼀个函数,⽤类名去调⽤,如果是⼀个额⽅法,⽤对象去调⽤
举例说明:
class Foo(object):
def __init__(self):
self.name="haiyan"
def func(self):
print(self.name)
obj = Foo()
obj.func()
Foo.func(obj)
判断函数和⽅法的⽅式
from types import FunctionType,MethodType
obj = Foo()
print(isinstance(obj.func,FunctionType)) #False
print(isinstance(obj.func,MethodType)) #True #说明这是⼀个⽅法
print(isinstance(Foo.func,FunctionType)) #True #说明这是⼀个函数。
print(isinstance(Foo.func,MethodType)) #False
⼆、js和jquery绑定事件的⼏种⽅式
三、创建表的⼀个limit_choices_to参数
limit_choices_to:屏蔽某些选项,只显⽰某些指定的选项。例如下⾯的,只让显⽰部门id是1001的
consultant = models.ForeignKey(verbose_name="课程顾问", to='UserInfo', related_name='consultant',limit_choices_to={'depart_id':1001})
四、include和inclusion_tag的区别
这两个都是处理代码冗余的,由于其他的页⾯也会有这样的功能,也要⽤到,我们可以吧它摘出来,在创建个⽂件夹写进去。导⼊进来
如果⽤include,这⾥⾯的数据得从后端传,
如果⽤inclusion_tag,你返回啥就会帮我们传啥,它既有⾃⼰的功能,也有include的功能,⼜可以处理数据
include的使⽤
<body>
<h3>添加页⾯</h3>
{% include "stark/form.html" %}
{#<form action="">#}
{# {{ form }}#}
{#</form>#}
#include导⼊的相当于下⾯注释的form表单的内容
inclusion_tag的使⽤
1、创建⼀个templatetags的⽂件夹,在⾥⾯创建⼀个change_form.py的⽂件,在⾥⾯写代码,需要加上
@register.inclusion_tag这个装饰器
#!usr/bin/env python
# -*- coding:utf-8 -*-
plate import Library
from django.urls import reverse
from app01.service.v1 import site
register = Library()
@register.inclusion_tag("stark/form.html")
def form(model_form_obj):
from django.forms import ModelChoiceField
from django.forms.boundfield import BoundField # 数据都封装在这个类了
new_form = []
for bfield in model_form_obj:
dic = {"is_popup": False, "item": bfield} # 每⼀个bfield就是Form的字段,是⼀个对象
if isinstance(bfield.field, ModelChoiceField):
# print(bfield.field,"popup按钮")
print(bfield, type(bfield)) # <class 'django.forms.boundfield.BoundField'>
releated_model_name = bfield.del # 到关联的类名
app_model_name = releated_model_name._meta.app_label, releated_model_name._del_name # 到应⽤名和类名(⽬的是拼接url) base_url = reverse("stark:%s_%s_add" % (app_model_name))
popup_url = "%s?_popupbackid=%s" % (base_url, bfield.auto_id) #每⼀个input框的id
print(bfield.auto_id,"111111")
dic["is_popup"] = True
dic["popup_url"] = popup_url
writeline和write的区别pythonnew_form.append(dic)
return {"form":new_form} #返回的这个form是给了"stark/form.html"它⾥⾯的form,然后循环遍历
3、使⽤
{% load change_form %}
<body>
<h3>编辑页⾯</h3>
{% form form %}
</body>
4、form.html
<form method="post" class="form-horizontal" novalidate>
{% csrf_token %}
{% for dic in form %}
<div class="col-sm-6">
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">{{ dic.item.label }}</label>
<div class="col-sm-10" >
{{ dic.item }}
{% if dic.is_popup %}
<div >
<a onclick="popUp('{{ dic.popup_url }}')" class="glyphicon glyphicon-plus"></a>
<!--注意要加引号,不然就会被当成变量了-->
</div>
{#判断如果是MOdelChoicesField是Fk#}
{#判断如果是MOdelChoicesField是Fk#}
{% endif %}
{{ s.0 }}
</div>
</div>
</div>
{% endfor %}
<div class="col-sm-offset-11 col-sm-1">
<input type="submit" class="btn btn-primary" value="提交">
</div>
</form>
<script>
function popupCallback(data) {
var op = ateElement("option");
op.value = data.id;
< = ;
op.setAttribute("selected", "selected");
}
function popUp(url) {
var popupPage = window.open(url, url, "status=1, height:500, width:600, toolbar=0, resizeable=0");
}
</script>
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论