Django   发布时间:2022-04-10  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了django学习笔记大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

<table class="text"><tr class="li1">
<td class="ln"><pre class="de1">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358

X)     print(y) 不换行输出     print(x,end=" ")     print(y,end=" ")     print()   import 和 from...import     导出某个函数 from somemodule import somefunction     导出多个函数 from somemodule import firstfunc,secondfunc,thirdfunc     导出全部函数 from somemodule import *   a = b = c = 1     三个变量分配到相同的内存空间 a,b,c = 1,2,"runoob"   python3六个数据类型     number,int,float,complex,int(),float(),complex()     String,    List,(列表) 数组     Tuple,(元组) 元组元素不可改变     Sets,(集合)是一个无需不重复元素的序列 set()     Dictionary(字典)就是js中的对象       type()不会认为子类是一种父类类型     isinstance()会认为子类是一种父类类型    数值的除法(/)总是返回一个浮点数,要获取整数,则需要用//操作符     is 判断两个标识符是不是引用同一个对象 is not   random() // 生成一个实数,范围在[0,1]之类 shuffle() // 所有元素随机排序 uniform(x,y) //   for in :      else:       a = ['Google','Baidu','Runoob','Taobao','QQ'] for i in range(len(a))     print(i,ap[i])   break语句可以跳出for和while的循环体 conTinue代表跳过该循环的剩余情况 pass是空语句,保持程序结构的完整性   迭代器的基本方法:iter()和next() ex: list = [1,3,4] it = iter(list) print(next(it)) >>>1 print(next(it)) >>>2   可以用for语句进行遍历 list = [1,4] it = iter(list) for x in it:     1 2 3 4   使用了yield的函数被称为生成器(generator) 生成器就是一个迭代器,在调用生成器运行的过程中,每次遇到yield 是函数会暂停并保存当前所有的运行信息,返回yield的值。并在下次执行 next()方法时从当前位置继续运行: ex:     import sys     def fibonacci(n):       a,c,counter = 0,1,0       while True:         if (counter > 0):           return         yield a         a,b = b,a + b         counter += 1     f = fibonacci(10)       while True:       try:         print(next(f),end = ' ')       except StopIteration:         sys.exit()   定义函数:   def Hello():     print("Hello world")     Hello()   >> Hello world   在python中String,tuples,numbers是不可更改的对象,而list,Dict是可以 修改的对象   不可变类型:变量赋值a = 5在赋值a=20,这里实际是新生成一个int对象10,   再让a指向它,而5被丢弃,不是改变a的值,相当于新生成了a   可变类型:变量赋值la=[1,4]后再赋值la[2]=5则是将list la   的第三个元素值更改,本身la没有变,只是内部的一部分值发生变化     python传不可变对象实例 def ChangeInt(a):   a = 10 b = 2 ChangeInt(b) print(b) # 结果是2    传可变对象实例  def changeme(mylist):    mylist.append([1,4])    print('函数内取值:',mylist)    return  调用changeme函数   mylist = [10,20,30]   changeme(mylist)   print('函数外取值:',mylist)   >>> 函数内取值:  [10,30,[1,4]]       函数外取值:  [10,4]]     必需参数、关键字参数、默认参数、不定长参数   def printme(str);prinTinfo(name,agE);   def prinTinfo(name,age = 35); def prinTinfo(arg1,*vartuplE)     匿名函数     sum = lambda arg1,arg2: arg1 + arg2;     变量作用域     L(Local): 局部作用域     E(Enclosing): 闭包函数外的函数中     G(Global): 全局作用域     B(Built-in): 内建作用域 ex:   x = int(2.9) # 内建作用域   g_count = 0   # 全局作用域   def outer():     0_count = 1 # 闭包函数外的函数中     def inner():       i_count = 2 # 局部作用域   python中只有模块module、类class以及函数def、lambda才会引入新的 作用域。也就是说除了这些语句内定义的变量,外部可以使用   if True:     msg = 'I am from Runoob'     msg:   >>> 'I am from Runoob'   当内部作用域想修改外部作用域的变量时,就要用到global和nonlocal关键字 num = 1 def fun1():   global num   print(num)   num = 123   print(num) fun1()   // 1 123   nonlocal.ex:   def outer():     num = 10     def inner():       nonlocal num       num = 100       print(num)     inner()     print(num)   outer()   >>> 100 100     数组方法     list.append(X)     list.insert(i,X)     list.remove(X)     list.pop([i])     list.clear()     list.index(X)     list.count(X) // x的次数     list.reverse()   列表推导式 >>> vec = [2,4,6] >>> [3*x for x in vec] [6,12,18] >>>[[x,x**2] for x in vec] [[2,4],[4,16],[6,36]] 对序列中的每个元素调用方法 freshfruit = ['  banana','  loganberry ','passion fruit  '] [weapon.Strip() for weapon in freshfruit] ['bAnner','loganberry','passion fruit'] 加入if过滤器   [3*x for x in vec if x > 3]   [12,18]   3*4的矩阵表转换为4*3列表 matrix = [ ...     [1,...     [5,6,7,8],...     [9,10,11,12],... ]   [[row[i] for row in matrix] for i in range(4)]   [[1,5,9],[2,10],[3,11],8,12]]   也可以通过下列方法来进行实现:     transposed = []     for i in range(4):       transposed.append([row[i] for row in matrix])     transposed.append(transposed_row)   del 语句   a = [1,66.25,333,1234.5]   del a[2:4]   a   [1,1234.5]   集合:是一个无序不重复元素的集   创建一个空集合,使用set()而不是{}   字典:    knights = {'gallahad': 'the pure','robin': 'the brave'}    for k,v in knights.items()       print(k,v)   同时遍历多个序列,可以用zip()组合:   questions = ['name','quest','favorite color'] >>> answers = ['lancelot','the holy grail','blue'] >>> for q,a in zip(questions,answers): ...     print('what is your {0}?  it is {1}.'.format(q,a)) what is your name?  it is lancelot. what is your quest?  it is the holy grail. what is your favorite color?  it is blue.   __name__属性:   一个模块被另一个程序第一次引入时,其主程序将运行。如果我们想在模块被引入时,   模块中的某一程序块不执行,我们可以用__name__属性来使该程序块仅在该模块自身   运行时执行   ex:   # Filename: using_name.py     if __name__ = '__main__':       print('程序自身在运行')     else:       print('我来自另一模块')   python using_name.py     >>>程序自身在运行      import using_name     >>>我来自另一模块     dir()函数:找到模块内定义的所有名称   python中的异常处理:   try:   except:   类的初始化:   def __init__(self): // 必须包含self,表示实例化对象     self.data = [] 实现继承:   class people   class student(peoplE)   实现多继承:   class DerivedClassName(Base1,Base2,Base3)         需要注意圆括号中父类的顺序,若是父类中有相同的方法名,而在子类使用时未   指定,python从左至右搜索 即方法在子类中未找到时,从左到右查找父类中是   否包含方法   __private_attrs: 私有属性   __private_method: 私有方法     re.match(pattern,String,flags)   @unique保证没有重复值     通过成员的名称来获取成员 Color['red'] 通过成员值来获取成员 Color(2)   动态语言和静态语言最大的不同,就是函数和类的定义,不是编译时定义的,而是运行时动态创建的。   type()函数既可以返回一个对象的类型,又可以创建出新的类型 用type创建class对象,tyep()函数依次传入三个参数: 1、class的名称 2、继承的父类集合 3、class的方法名称和函数绑定   metaclass控制类的创建行为   __new__()方法接收到的参数依次是: 1、当前准备创建的类的对象 2、类的名字 3、类继承的父类集合 4、类的方法集合   class ListMetaclass(typE):   def __new__(cls,name,bases,attr):     attrs['add'] = lambda self,value: self.append(value)     return type.__new__(cls,attrs)   class MyList(list,metaclass=ListMetaclass):   pass   L = MyList() l.add(1) L [1] 而普通的list没有add()方法 L2 = list() L2.add(1) #error         ORM全称 'Object Relational Mapping',即对象-关系映射     os.path.join()连接目录和文件名 verbose_name workon查看虚拟环境列表 workon test 进入虚拟环境   CharField EmailFiled ForeighKey datetiR_441_11845@eFild IntegerField IPadressField FileField ImageField max_length最大长度 null可以为空 default 默认值   db_table ordering自动排序 verbose_name_plural 复数信息   Usermessage.objects.all() 返回django的内置类型 可以使用for循环   Usermessage.object.filter(name='mike')    all_message = Usermessage.objects.filter(name='mike')     # all_message.delete()     for message in all_message:         message.delete()     # if request.method == 'POST':     #     NAME = request.POST.get('name','')     #     message = request.POST.get('message','')     #     address = request.POST.get('address','')     #     EMAIl = request.POST.get('email','')     #     user_message = Usermessage()     #     user_message.name = name     #     user_message.message = message     #     user_message.address = address     #     user_message.email = email     #     user_message.object_id = 'Hellowodld3'     #     user_message.save()大佬总结

以上是大佬教程为你收集整理的django学习笔记全部内容,希望文章能够帮你解决django学习笔记所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: