常用的模板标签与过滤器
常用的模板标签{% %}
循环:for
条件:if(可逻辑判断)、ifequal、ifnotequal
链接:url ({% url xxx.xx %}这亚子用)
模板嵌套:block、extends、include
注释:{# #}
常用的过滤器
日期:date
长度:length
字数截取:truncatechars、truncatechars_html、truncatewords、truncatewords_html
对数量的过滤器: <h3>一共有{{ blogs|length }}篇博客</h3> 对时间格式的过滤器: <tr><td>创建时间</td><td>{{ blog.creat_time|date:"Y-m-d H:i:s" }}</td></tr> 对内容长度的过滤器:<p>{{ blog.content|truncatechars:30 }}</p>{#通过过滤器显示摘要,还有其他方法#}
静态文件
在全局setting里面加上
STATIC_URL = '/static/' # 下面这个这个静态文件目录一定要注意,这样项目才能访问的到静态文件 STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'static') ]
然后再引用静态文件的地方进行
<link rel="stylesheet" href="/static/index.css"> 或者 {% load static %} <link rel="stylesheet" href="{% static 'index.css' %}">
一定要注意,在django 3.0中要使用{% load static %}而不是{% load staticfiles %}