django 文件上传
1 更改上传文件的丑陋的格式
{% csrf_token %}NB上传
2 后台处理文件上传
defupload(request):if request.method=='GET':return render(request,'app01/upload.html')if request.method=='POST':#print(request.POST.get('username'))
#print(request.FILES.get('img'))
img=request.FILES.get('img') #img是对象,包含了文件的大小,名称,文件内容
f=open(img.name,'wb')for line inimg.chunks():
f.write(line)
f.close()return HttpResponse('upload file')
django ORM的操作
1跨表操作
model.Person.objects.all().select_related('ut') #如果操作有跨表的动作的话
model.Person.objects.all().select_related('ut','nt')
2GROUP BY的操作
from django.db.models importCount, Min, Max, Sum
models.Tb1.objects.filter(c1=1).values('id').annotate(c=Count('num'))
SELECT"app01_tb1"."id", COUNT("app01_tb1"."num") AS "c" FROM "app01_tb1" WHERE "app01_tb1"."c1" = 1 GROUP BY "app01_tb1"."id"
3distinct 操作
model.UserInfo.objects.values('data').distinct()
4order_by操作
model.UserInfo.objects.all().order_by('id',''name)
5extra操作
Entry.objects.extra(select={'new_id': "select col from sometable where othercol > %s"}, select_params=(1,))
Entry.objects.extra(where=["foo='a' OR bar = 'a'", "baz = 'a'"])
6only操作
model.UserInfo.objects.filter(...).only('id','name')
7using操作
model.Person.objects.all().using('default')# 需要在settings.py 中定义数据库的连接字符串
ajax学习
原生的ajax是XMLHttpRequest对象
function AjaxSubmit2() {
var xhr=new XMLHttpRequest();
xhr.onreadystatechange=function () {if(xhr.readyState == 4){//接收完毕服务器返回的数据
console.log(xhr.responseText);
}
};
xhr.open('GET','/ajax1.html?p=123');
xhr.send(null);
}
原生的ajax发送post请求 数据放在send方法中,需要设置请求头.
function AjaxSubmit4() {
var xhr=new XMLHttpRequest();
xhr.onreadystatechange=function () {if(xhr.readyState == 4){//接收完毕服务器返回的数据
console.log(xhr.responseText);
}
};
xhr.open('POST','/ajax1.html');
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset-UTF-8');
xhr.send("p=456");
}