Django 配置HTML 模板操作

作者: print("") 分类: python 发布时间: 2018-07-26 22:03

Django 配置HTML 模板操作

首先呢。建立几个URL对应关系

url.py

urlpatterns = [
    url(r'^tpl1/', views.tpl1),
    url(r'^tpl2/', views.tpl2),
    url(r'^tpl3/', views.tpl3),
]

views.py

def tpl1(request):
    user_list=[1,2,3,4,5]
    return render(request,'tpl1.html',{'u':user_list})

def tpl2(request):
    name='root'
    return render(request,'tpl2.html',{'name':name})

def tpl3(request):
    status="已经删除"
    return render(request,'tpl3.html',{'status':status})

然后在templast 目录中建立一个模板的文件例如:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="/static/comm.css"  />
    <title>{% block title %} {% endblock %}</title>
    <style>
        .pg-header{
            height: 48px;
            background-color: seashell;
            color: green;
        }

    </style>
    {% block cs %} {% endblock %}
</head>
<body>
    <div class="pg-header">管理=.=</div>
    {% block content %} {% endblock %}
<script src="/static/comm.js"></script>
{% block js %} {% endblock %}
</body>
</html>

首先说下:

block 表示这个是一个模板  

title  表示名称

enblock 表示接受

里面有两个block 第一个是头部信息 第二个是内容信息放置位置

那么怎么去调用这个模板呢。如下:

{% extends 'master.html' %}
{% block title %}
    用户管理
 {% endblock %}
{% block content %}
    <h1>用户管理</h1>
    <ul>

        {% for i in u %}
            <li>{{ i }}</li>
        {% endfor %}

    </ul>
{% endblock %}

extends 是表明调用的是master.html 的模板

block title 是头部信息进行填充

block content 是填充模板中的 block content 

tpl2.html

{% extends 'master.html' %}
{% block title %}
    用户管理
 {% endblock %}
{% block content %}
    <h1>修改密码{{ name }}</h1>
{% endblock %}

tpl3.html

{% extends 'master.html' %}
{% block title %}
    用户管理
 {% endblock %}
{% block content %}
    <h1>{{ status }}</h1>
{% endblock %}

小模块的include 

新建一个tag.html

<form>
    <input type="text" />
    <input type="submit" />
</form>

在tpl1.html 中直接导入就行了=。= 

{% include 'templates/tag.html' %}

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!

发表评论

您的电子邮箱地址不会被公开。