DTL与HTML的区别
DTL作为模板使用,是一种特殊语法既可以被django编译的html文件,实现数据动态化。
渲染模板
- reder
直接将模板传递进去,省去返回HttpResponse的步骤,使用时在根目录的templates文件夹中创建html文件
from django.shortcuts import render
def index(request):
return render(request,'index.html')
- render_to_string
需要在django.template.loader下导入,而且返回一个HttpResponse
from django.template.loader import render_to_string
from django.http import HttpResponse
def index(request):
html = render_to_string("index.html")
return HttpResponse(html)
模板查找路径
其实在setting中django已经将templates文件路径配置
首先将当前项目的路径动态获取到BASE_DIR中。
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
在temolates中将templates路径寻找到
TEMPLATES = [
{
'DIRS': [os.path.join(BASE_DIR, 'templates')]
}
- 使用reder渲染模板的时候,根目录下的templates文件夹为优先级即优先在DIRS中寻找
'DIRS': [os.path.join(BASE_DIR, 'templates')]
- 如果将DIRS中的路径删除
'DIRS': []
- 这个时候则会在 APP_DIRS中寻找,且优先在自己app中的templates的寻找,如果自己app中没有找到则去别的app中寻找。(注意这里所说的app必须在 INSTALLED_APPS表明)
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'front'
]
DTL模板—变量
- 使用ctrl+B跳转到reder中发现该函数可以传递参数context
context = {
'username':'BingYiii'
}
return render(request,'index.html',context=context)
- 这时只要在html中使用{{参数}}即可将context中的参数传递进去(注意context为字典的格式)
{{ username }}
-可以向context字典的键值中传入对象
class Person(object):
def __init__(self,username):
self.username = username
p = Person("BingYiii")
context = {
'person':p
}
return render(request,'index.html',context=context)
-这时可以在html中直接写入方法
{{ person.username }}
DTL模板—标签
- if标签
所有的标签都在{}之间实现,
if标签有闭合标签endif
{% if %}
# 使用pycharm在输入if后按下Tab键会自动弹出两行代码
{% endif %}
- for…in标签
比python中for…in标签多了empty语句。
<ur> # 无序列表标签
{% for comment in comments %}
<li>{{ comment }}</li> # li标签是一个元素标签,可以用在有序列表<ol>和无序列表<li>中
{% empty %}
<li>{{ '没有评论' }}</li>
{% endfor %}
</ur>
<table> # 表格标签
<thead> # 定义表格的表头与<tbody>或者<tfoot>一同使用
<tr> # 定义表格中的一行,单元格容器,每行可以容纳多个单元格
<td>序号</td> # td标签和th标签用来定义单元格,所有单元格都在tr标签内,每个单元格由一对< td>和< /td>标签或一对< th>和< /th>标签表示,具体的表格内容放置在这一对td标签或th标签之中,
<td>书名</td>
<td>作者</td>
<td>价格</td>
</tr>
</thead>
<tbody> # 对表格的主题内容进行分组
{% for book in books %}
{% if forloop.first %}
<tr style="background: red">
{% elif forloop.last %}
<tr style="background: blue">
{% else %}
<tr>
{% endif %}
<td>{{ forloop.counter0 }}</td>
<td>{{ book.name }}</td>
<td>{{ book.author }}</td>
<td>{{ book.price }}</td>
</tr>
{% endfor %}
</tbody>
</table>
-
with标签
with标签(将名称替换)只能在{%with%}{%endwith%}中使用,不能在外部使用{% with persons.0 as zs %} <p>{{ zs }}</p> <p>{{ zs }}</p> <p>{{ zs }}</p> {% endwith %}
-
url标签
<style>
.nav{
overflow: hidden;
}
.nav li{
float: left;
list-style: none;
margin: 0 20px;
}
</style>
<body>
<ur class="nav">
<li><a href="/">首页</a></li>
<li><a href="{% url 'book' %}">读书</a></li>
<li><a href={% url 'movie' %}>电影</a></li>
<li><a href="{% url 'city' %}">同城</a></li>
<li><a href="{% url 'detail' book_id=1 %}">最火的的博客</a></li> # 传递多个参数,那么通过空格的方式进行分隔
<li><a href="{% url 'login' %}?next=/">登录</a></li>
</ur>
- autoescape标签
DTL已经自动将字符进行转义
如果不需要转义那么输入如下代码:用来关掉自动转义
{% autoescape off %}
{{info}}
{% endautoescape %}