0%

Django开发学习笔记

记录使用django开发过程中遇到的问题

djanp url参数

django中有时候需要在url中传入参数用来生成特定的url来指定某个具体对象。

url参数转化器有如下几种:

  1. str:除了斜杠/以外所有的字符都是可以的。
  2. int:只有是一个或者多个的阿拉伯数字。
  3. path:所有的字符都是满足的。
  4. uuid:只有满足uuid.uuid4()这个函数返回的字符串的格式。
  5. slug:英文中的横杆或者英文字符或者阿拉伯数字或者下划线才满足。

前端数据读取

与python对dict的读取不同,html文件对dict数据直接使用.来获取:

1
2
3
4
5
# python 
dict = {'a': 1, 'b': 2, 'b': '3'}
print(dict['a'] )
# html
dict.a

django form表单

django从前端读取表单数据时,默认情况下要求表单数据不为空,当表单某字段数据为空时,form.is_vaild将返回False

Field.required
By default, each Field class assumes the value is required, so if you pass an empty value – either None or the empty string (“”) – then clean() will raise a ValidationError exception:

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
>>> from django import forms
>>> f = forms.CharField()
>>> f.clean('foo')
'foo'
>>> f.clean('')
Traceback (most recent call last):
...
ValidationError: ['This field is required.']
>>> f.clean(None)
Traceback (most recent call last):
...
ValidationError: ['This field is required.']
>>> f.clean(' ')
' '
>>> f.clean(0)
'0'
>>> f.clean(True)
'True'
>>> f.clean(False)
'False'
To specify that a field is not required, pass required=False to the Field constructor:

>>> f = forms.CharField(required=False)
>>> f.clean('foo')
'foo'
>>> f.clean('')
''
>>> f.clean(None)
''
>>> f.clean(0)
'0'
>>> f.clean(True)
'True'
>>> f.clean(False)
'False'

If a Field has required=False and you pass clean() an empty value, then clean() will return a normalized empty value rather than raising ValidationError. For CharField, this will be an empty string. For other Field classes, it might be None. (This varies from field to field.)
Widgets of required form fields have the required HTML attribute. Set the Form.use_required_attribute attribute to False to disable it. The required attribute isn’t included on forms of formsets because the browser validation may not be correct when adding and deleting formsets.

通过为字段添加required=False,可以允许字段内容为空

js动态添加删除表单

在页面中通过点击按钮来实现表单的增加和删除,以下为实现方式。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<form id = "form" >

<div id = "servicebody">
<div class="well" id = "con">
<div class="form-group">
<label for="serviceName">服务名称</label>
<input type="text" id="servideName">
</div>
</div>
</div>
<!-- 在foradd之前添加服务 -->
<div id = "forAdd"></div>
<button type="submit" class="btn btn-info " style="float:right">创建应用</button>
<button class="btn btn-default" id = "addService" style="float:right">添加服务</button>
<br>
</form>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24


//删除表单
var service_div = 1;
$("#addService").click(function () {
//添加的内容
var e = document.getElementById("servicebody"); //获取要添加的内容
var div = document.createElement("div");
div.id = "service" + service_div;
div.innerHTML = e.innerHTML; //将内容存放在新建的div中
var del = document.createElement('p');
del.innerHTML = '<button class="btn btn-danger btn-xs" id = "rmService'
+ service_div + '"onclick="del_service(this.id)">删除</button>'; //添加删除按钮
div.children.con.appendChild(del);
$("#forAdd").before($(div));
service_div++; //为添加的表单设置不同id
});

function del_service(eleId) {
debugger
var eeid = document.getElementById(eleId).parentNode.parentNode.parentNode.id; //获取要删除的元素的id
var ee = document.getElementById(eeid);
document.getElementById("form").removeChild(ee); //删除元素
}