2021.5.21
now 8:54
1、json序列化隐藏字段
2、读取静态文件
3、django引入静态文件
4、模板渲染底层原理
5、模板标签语法
6、过滤器
7、自定义过滤器
8、全局上下文
9、模板继承_CSRF原理
1、8:55--9:15 json序列化隐藏字段
2、9:20--10:14 读取静态文件
class StaticView(View):
def get(self, request, i, *args, **kwargs):
import re
import os
# 获取文件名
filename = i
filedir = os.path.join(os.getcwd(),'static\\images', filename)
if not os.path.exists(filedir):
raise Http404()
# mimetypes
return FileResponse(open(filedir, 'rb'),content_type='image/png')
3、10:15--10:43 django引入静态文件
settings
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static\images'),
os.path.join(BASE_DIR, 'static\css'),
os.path.join(BASE_DIR, 'static\js'),
]
html
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="{% static 'my.css' %}" type="text/css">
<script type="text/javascript" src="{% static 'my.js' %}"></script>
</head>
<body>
<img src="/static/1.png">
<img src="{% static '1.png' %}">
<form action="/student/" method="post">
{% csrf_token %}
<input type="submit" id="btn">
</form>
</body>
</html>
4、13:40--14:24 模板渲染底层原理
class IndexView(View):
def get(self, request):
import os
with open(os.path.join(os.getcwd(), 'templates\login.html'), 'rb') as fr:
content = fr.read()
t = Template(content)
c = Context({'uname': 'lisi'})
str = t.render(c)
return HttpResponse(str)
class Index1View(View):
def get(self, requset):
t = loader.get_template('login.html')
str = t.render({'uname': 'wangwu'})
return HttpResponse(str)
5、14:25--15:10 模板标签语法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{# 单行注释 #}
{% comment %}
多
行
注
释
{% endcomment %}
<h3>获取views传值</h3>
<ol>
<li>{{ user.uname }}</li>
<li>{{ numlist.2 }}</li>
<li>{{ current.year }}</li>
<li>{{ current.date }}</li>
<li>{{ current.time }}</li>
<li>{{ str.upper }}</li>
</ol>
{% for foo in numlist reversed%}
{{ forloop.revcounter0 }}--{{ foo }}<br>
{% endfor %}
<br>
<hr>
{% for k,v in user.items %}
{{ k }}--{{ v }}<br>
{% endfor %}
<br>
<hr>
{% for foo in list %}
{% empty %}
无记录
{% endfor %}
</body>
</html>
6、15:55--·17:00 过滤器
7、20:42--20:55 自定义过滤器
8、21:00--21:55 全局上下文
9、22:00--22:30 模板继承_CSRF原理