zoukankan      html  css  js  c++  java
  • 函数和方法的区别

    一、函数和方法的区别

    1、函数要手动传self,方法不用传

    2、如果是一个函数,用类名去调用,如果是一个额方法,用对象去调用

    举例说明:

      1 class Foo(object):
      2     def __init__(self):
      3         self.name="haiyan"
      4     def func(self):
      5         print(self.name)
      6 
      7 obj = Foo()
      8 obj.func()
      9 Foo.func(obj)

    判断函数和方法的方式

      1 from types import FunctionType,MethodType
      2 obj = Foo()
      3 print(isinstance(obj.func,FunctionType))  #False
      4 print(isinstance(obj.func,MethodType))   #True   #说明这是一个方法
      5 
      6 print(isinstance(Foo.func,FunctionType))  #True   #说明这是一个函数。
      7 print(isinstance(Foo.func,MethodType))  #False

     

    二、js和jquery绑定事件的几种方式

    三、创建表的一个limit_choices_to参数

    limit_choices_to:屏蔽某些选项,只显示某些指定的选项。例如下面的,只让显示部门id是1001的

      1 consultant = models.ForeignKey(verbose_name="课程顾问", to='UserInfo', related_name='consultant',limit_choices_to={'depart_id':1001})

     四、include和inclusion_tag的区别

    这两个都是处理代码冗余的,由于其他的页面也会有这样的功能,也要用到,我们可以吧它摘出来,在创建个文件夹写进去。导入进来

      1 如果用include,这里面的数据得从后端传,
      2 如果用inclusion_tag,你返回啥就会帮我们传啥,它既有自己的功能,也有include的功能,又可以处理数据

    include的使用

      1 <body>
      2 <h3>添加页面</h3>
      3 {% include "stark/form.html" %}
      4 {#<form action="">#}
      5 {#    {{ form }}#}
      6 {#</form>#}
      7 
      8 
      9 #include导入的相当于下面注释的form表单的内容

    inclusion_tag的使用

    1、创建一个templatetags的文件夹,在里面创建一个change_form.py的文件,在里面写代码,需要加上

    @register.inclusion_tag这个装饰器
      1 #!usr/bin/env python
      2 # -*- coding:utf-8 -*-
      3 from django.template import Library
      4 from django.urls import reverse
      5 from stark.service.v1 import site
      6 
      7 register = Library()
      8 @register.inclusion_tag("stark/form.html")
      9 def form(model_form_obj):
     10     from django.forms import ModelChoiceField
     11     from django.forms.boundfield import BoundField  # 数据都封装在这个类了
     12     new_form = []
     13     for bfield in model_form_obj:
     14         dic = {"is_popup": False, "item": bfield}  # 每一个bfield就是Form的字段,是一个对象
     15         if isinstance(bfield.field, ModelChoiceField):
     16             # print(bfield.field,"popup按钮")
     17             print(bfield, type(bfield))  # <class 'django.forms.boundfield.BoundField'>
     18             releated_model_name = bfield.field.queryset.model  # 找到关联的类名
     19             app_model_name = releated_model_name._meta.app_label, releated_model_name._meta.model_name  # 找到应用名和类名(目的是拼接url)
     20             base_url = reverse("stark:%s_%s_add" % (app_model_name))
     21             popup_url = "%s?_popupbackid=%s" % (base_url, bfield.auto_id)  #每一个input框的id
     22             print(bfield.auto_id,"111111")
     23             dic["is_popup"] = True
     24             dic["popup_url"] = popup_url
     25         new_form.append(dic)
     26     return {"form":new_form}   #返回的这个form是给了"stark/form.html"它里面的form,然后循环遍历
    View Code

    3、使用

      1 {% load change_form %}
      2 <body>
      3 <h3>编辑页面</h3>
      4 {% form form %}
      5 </body>

    4、stark/form.html

      1 <form method="post" class="form-horizontal" novalidate>
      2     {% csrf_token %}
      3     {% for dic in form %}
      4         <div class="col-sm-6">
      5             <div class="form-group">
      6                 <label for="inputEmail3" class="col-sm-2 control-label">{{ dic.item.label }}</label>
      7                 <div class="col-sm-10" style="position: relative">
      8                     {{ dic.item }}
      9                     {% if dic.is_popup %}
     10                         <div style="position: absolute;right: -5px;top: 8px;z-index: 9999">
     11                             <a onclick="popUp('{{ dic.popup_url }}')" class="glyphicon glyphicon-plus"></a>  <!--注意要加引号,不然就会被当成变量了-->
     12 
     13                         </div>
     14                         {#                    判断如果是MOdelChoicesField是Fk#}
     15                         {#                    判断如果是MOdelChoicesField是Fk#}
     16                     {% endif %}
     17                     {{ dic.item.errors.0 }}
     18                 </div>
     19             </div>
     20         </div>
     21     {% endfor %}
     22     <div class="col-sm-offset-11 col-sm-1">
     23         <input type="submit" class="btn btn-primary" value="提交">
     24     </div>
     25 </form>
     26 <script>
     27     function popupCallback(data) {
     28         var op = document.createElement("option");
     29         op.value = data.id;
     30         op.text = data.text;
     31         op.setAttribute("selected","selected");
     32         document.getElementById(data.popupbackid).appendChild(op)
     33     }
     34     function popUp(url) {
     35            var popupPage = window.open(url, url, "status=1, height:500, 600, toolbar=0, resizeable=0");
     36     }
     37 
     38 </script>

     

     

     

    归类 :  Django

  • 相关阅读:
    2018-8-10-如何写毕业论文-表格
    2018-8-10-win10-uwp-自定义控件初始化
    2018-8-10-win10-uwp-自定义控件初始化
    hashMap的hashCode() 和equal()的使用
    java中fail-fast 和 fail-safe的区别
    java各种集合的线程安全
    Java集合框架总结—超详细-适合面试
    CodeForces 1058C C. Vasya and Golden Ticket
    CodeForces 1058C C. Vasya and Golden Ticket
    CodeForces-1058B B. Vasya and Cornfield
  • 原文地址:https://www.cnblogs.com/lz1996/p/11568230.html
Copyright © 2011-2022 走看看