页面解析
过滤器 |
- 一定注意不要多加空格,否则不会执行
显示日期时间
-
| date:‘Y-m-d H-m-s’
- Y 显示4位
- y 显示2位
- H 24小时制
- h 12小时制
-
在数据库中时间会比实际中少8个小时,如果在数据库内部操作需要注意
变换大小写
- | upper 变大写
- | lower 变小写
判断整除
- | divisibleby:2
- 表示前面点的数除以2 ,如果整除返回真,否则返回假
加
- | add:10
- 表示给前面的数字加10
注释
- 单行注释
- {# #}
- 多行注释
- {% comment %}
- {% endcomment %}
计算
- widthratio 数值 分母 分子
- 表示这个数值乘以分子除以分母
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{% for stu in stus %}
计数从0开始{{ forloop.counter0 }}
计数从1开始{{ forloop.counter }}
计数从最后开始到1结束{{ forloop.revcounter }}
计数从最后开始到0结束{{ forloop.revcounter0 }}
{% ifequal forloop.counter 3 %}
<h1>姓名:{{ stu.stu_name }}</h1>
{% else %}
姓名:{{ stu.stu_name | upper }}
{% endifequal %}
性别:
{% if stu.stu_sex %}
男
{% else %}
女
{% endif %}
生日:{{ stu.stu_birth | date:'Y-m-d'}}
创建时间:{{ stu.stu_create_time | date:'y-m-d H:m:s'}}
<h3>语文成绩 + 10:</h3>{{ stu.stu_yuwen | add:10 }}输出时加了10分
语文成绩乘以10: {% widthratio stu.stu_yuwen 1 10 %}
数学成绩:{{ stu.stu_math | divisibleby:2}}
<br />
{% empty %}
没有学生信息
{% endfor %}
</body>
</html>
定义name
- 因为url的信息,在以后的应用过程中,会因客户需求的变化而更改,所以需要给每个url后定义一个别名,在写地址的时候用别名,避免多处修改的问题
- {% url ‘namespace:name’ 参数 %}
- 工程url中定义namespace
- 模块appurl中定义name
<body>
{% for cls in class %}
id: {{ cls.id }}
<a href="{% url 's:stu' cls.id %}">班级:{{ cls.c_name }}</a>
描述:{{ cls.c_desc }}
<br />
{% endfor %}
</body>
GET 传参的方式
url(r'^showstu/(\d+)/', views.showStudent, name='stu'),
url(r'^redirectStu/(?P<g_id>\d+)', views.redirectStu, name='red'),
# /s/allstu/2018/4/1/
url(r'^selstu/(\d+)/(\d+)/(\d+)/', views.selStu),
url(r'^actstu/(?P<year>\d+)/(?P<month>\d+)/(?P<day>\d+)/', views.actStu),
重定向
- HttpResponseRedirect()
- 如果需要给其他函数传递参数需要用到reverse方法导入路径如下
- from django.core.urlresolvers import reverse
def showStudent(request, g_id):
# stus = Student.objects.filter(g_id=g_id)
# 不包含这个数据的所有信息
# stus = Student.objects.exclude(c_id=c_id)
# return render(request, 'show_student.html', {'students' : stus})
# 业务逻辑
return HttpResponseRedirect(
reverse('s:red', kwargs={'g_id': g_id})
)
def redirectStu(request, g_id):
stus = Student.objects.filter(g_id=g_id)
return render(request, 'show_student.html', {'students': stus})
# selstu/(\d+)/(\d+)/(\d+)/ 只是传三个参数
def selStu(request, y, m, d):
return HttpResponse('获取url传递多个参数的方法%s,%s,%s' % (y, m, d))
# actstu/(?P<year>\d+)/(?P<month>\d+)/(?P<day>\d+)/ 这个是指定名字的,一个名字对应一个值
def actStu(request, month, year, day):
return HttpResponse('HELLO %s,%s,%s' % (month, year, day))
404 、500错误显示
- 404 一般是网址输入错误
- 500 一般是后台代码错误
- 在工程目录下的urls.py中,加上下面两段,后面的值是要调用的函数
handler404 = page_not_found # handler404 指定处理404错误的关键字
handler500 = server_error # handler500 指定处理500错误的关键字
- 在工程目录下的settings.py中
- 将原来的DEBUG = True 改为 False
- 这样如果网页出现错误就不再显示debug
DEBUG = False # 可以显示404.500的错误
ALLOWED_HOSTS = ['*'] # 允许所有人访问
请求方式
-
post 提交数据隐藏了
-
get 提交数据在url上,?xx=xxx 每个浏览器对后面参数长度有限制
-
put 更新全部
-
patch 更新局部数据
-
delete 删除
-
删除: Student.objects.fillter(s_id=s_di).delete()
-
code是状态码
-
code: 200 请求成功
-
pk == id
数据删除和更新
<body>
{% for student in students %}
id : {{ student.id }}
姓名 :{{ student.stu_name }}
<a href="/appstu/delstu/?stu_id={{ student.id }}">删除</a>
<a href="/appstu/upstu/?stu_id={{ student.id }}">修改</a>
<br />
{% endfor %}
</body>
def delStu(request):
stu_id = request.GET.get('stu_id')
Student.objects.filter(id=stu_id).delete()
return HttpResponseRedirect('/appclass/showcls/')
def upStu(request):
stu_id = request.GET.get('stu_id')
# stu = Student.objects.get(id=stu_id)
# stu.s_name = '修改的名字'
# stu.save()
Student.objects.filter(id=stu_id).update(stu_name='大乔')
return HttpResponseRedirect('/appclass/showcls/')