模板语言
render(request, "模板的文件的路径", {'obj': 1234, 'k1': [1,2,3,4],"k2": {'name': '张扬','age': 73}})
<html>
<body>
<h1> {{ obj }} </h1>
<h1> {{ k1.3 }} </h1>
<h1> {{ k2.name }} </h1>
{% for i in k1 %}
<p> {{ i }} </p>
{% endfor %}
{% for row in k2.keys %}
{{ row }}
{% endfor %}
{% for row in k2.values %}
{{ row }}
{% endfor %}
{% for k,v in k2.items %}
{{ k }} - {{v}}
{% endfor %}
</body>
</html>
ORM
a. 创建类和字段
class User(models.Model):
age = models.IntergerFiled()
name = models.CharField(max_length=10)#字符长度
Python manage.py makemigrations
python manage.py migrate
# settings.py 注册APP
b. 操作
增
models.User.objects.create(name='qianxiaohu',age=18)
dic = {'name': 'xx', 'age': 19}
models.User.objects.create(**dic)
obj = models.User(name='qianxiaohu',age=18)
obj.save()
删
models.User.objects.filter(id=1).delete()
改
models.User.objects.filter(id__gt=1).update(name='alex',age=84)
dic = {'name': 'xx', 'age': 19}
models.User.objects.filter(id__gt=1).update(**dic)
查
models.User.objects.filter(id=1,name='root')
models.User.objects.filter(id__gt=1,name='root')
models.User.objects.filter(id__lt=1)
models.User.objects.filter(id__gte=1)
models.User.objects.filter(id__lte=1)
models.User.objects.filter(id=1,name='root')
dic = {'name': 'xx', 'age__gt': 19}
models.User.objects.filter(**dic)
v1 = models.Business.objects.all()
# QuerySet ,内部元素都是对象
# QuerySet ,内部元素都是字典
v2 = models.Business.objects.all().values('id','caption')
# QuerySet ,内部元素都是元组
v3 = models.Business.objects.all().values_list('id','caption')
后台渲染页面时返回代码:
render(request, "模板的文件的路径", {'obj': 1234, 'k1': [1,2,3,4],"k2": {'name': '张扬','age': 73}})
<html>
<body>
<h1> {{ obj }} </h1>
<h1> {{ k1.3 }} </h1>
<h1> {{ k2.name }} </h1>
{% for i in k1 %}
<p> {{ i }} </p>
{% endfor %}
{% for row in k2.keys %}
{{ row }}
{% endfor %}
{% for row in k2.values %}
{{ row }}
{% endfor %}
{% for k,v in k2.items %}
{{ k }} - {{v}}
{% endfor %}
</body>
</html>
ORM
a. 创建类和字段
class User(models.Model):
age = models.IntergerFiled()
name = models.CharField(max_length=10)#字符长度
Python manage.py makemigrations
python manage.py migrate
# settings.py 注册APP
b. 操作
增
models.User.objects.create(name='qianxiaohu',age=18)
dic = {'name': 'xx', 'age': 19}
models.User.objects.create(**dic)
obj = models.User(name='qianxiaohu',age=18)
obj.save()
删
models.User.objects.filter(id=1).delete()
改
models.User.objects.filter(id__gt=1).update(name='alex',age=84)
dic = {'name': 'xx', 'age': 19}
models.User.objects.filter(id__gt=1).update(**dic)
查
models.User.objects.filter(id=1,name='root')
models.User.objects.filter(id__gt=1,name='root')
models.User.objects.filter(id__lt=1)
models.User.objects.filter(id__gte=1)
models.User.objects.filter(id__lte=1)
models.User.objects.filter(id=1,name='root')
dic = {'name': 'xx', 'age__gt': 19}
models.User.objects.filter(**dic)
v1 = models.Business.objects.all()
# QuerySet ,内部元素都是对象
# QuerySet ,内部元素都是字典
v2 = models.Business.objects.all().values('id','caption')
# QuerySet ,内部元素都是元组
v3 = models.Business.objects.all().values_list('id','caption')
后台渲染页面时返回代码:
return render(request,{“v2”:v2.first()})
# 获取到的一个对象,如果不存在就报错
models.Business.objects.get(id=1)
对象或者None = models.Business.objects.filter(id=1).first()
外键:
v = models.Host.objects.filter(nid__gt=0)
v[0].b.caption ----> 通过.进行跨表
外键:
class UserType(models.Model):
caption = models.CharField(max_length=32)
id caption
# 1,普通用户
# 2,VIP用户
# 3, 游客
class User(models.Model):
age = models.IntergerFiled()
name = models.CharField(max_length=10)#字符长度
# user_type_id = models.IntergerFiled() # 约束,
user_type = models.ForeignKey("UserType",to_field='id') # 约束,
name age user_type_id
Ajax
$('#ajaxsubmit').click(function(){
$.ajax({
url:"/test_ajax",
type:'POST',
data:{'hostname':$('#hostname').val(),'ip':$('#ip').val(),'port':$('#port').val(),'sb_id':$('#sb_id').val()},
success:function(data){
//data是服务器返回的字符串,这里可以看成是一个回调函数
var obj = JSON.parse(data);
if (obj.status){
location.reload();
}
else {
$('#error_msg').text(obj.error)
}
}
})
})
建议:永远让服务器返回一个字典
return HttpResponse(json.dumps(字典))
$('#ajaxsubmit').click(function(){
$.ajax({
url:"/test_ajax",
type:'POST',
data:{'hostname':$('#hostname').val(),'ip':$('#ip').val(),'port':$('#port').val(),'sb_id':$('#sb_id').val()},
success:function(data){
//data是服务器返回的字符串,这里可以看成是一个回调函数
var obj = JSON.parse(data);
if (obj.status){
location.reload();
}
else {
$('#error_msg').text(obj.error)
}
}
})
})
建议:永远让服务器返回一个字典
return HttpResponse(json.dumps(字典))