django-模板

django的模板不仅仅是一个html文件,还可以设置参数,嵌入编程语句。
django 3 可以在settings.py中自动添加以下配置信息:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')]  # 配置模板,将上面定义的BASE_DIR和模板文件连接
        ,
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]
1. 创建模板文件

在这里插入图片描述

2. 模板文件
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<h1>这是一个模板文件</h1>
<li>加载模板文件</li>
	<ol>去模板目录下面获取html文件的内容,得到一个模板对象</ol>
<li>定义模板上下文</li>
	<ol>向模板文件传递数据</ol>
<li>模板渲染</li>
	<ol>得到一个标准的html内容</ol>
</body>
</html>
3. 创建视图
def index(request):
    '''
    处理请求并和M,T进行交互
    :param request:
    :return:
    '''
    # 使用模板文件
    # 1. 加载模板文件
    temp = loader.get_template('goods/index.html')  # 要加文件后缀
    # 2. 定义模板上下文
    context = RequestContext(request, {})  # 上面传入的request对象,后一个参数是一个字典
    # 3. 模板渲染,产生标准的html内容
    context = {'goods': 'goods'}  # context必须是一个字典类型
    res_html = temp.render(context)  # 生成渲染的内容
    # 4.返回给浏览器
    return HttpResponse(res_html)
4.runserver

在这里插入图片描述

from django.shortcuts import render
from django.http import HttpResponse
from django.template import loader, RequestContext

# http://127.0.0.1:8000/index
def index(request):
    '''
    处理请求并和M,T进行交互
    :param request:
    :return:
    '''
    # 使用模板文件
    # 1. 加载模板文件
    temp = loader.get_template('goods/index.html')  # 要加文件后缀
    # 2. 定义模板上下文
    context = RequestContext(request,{})   # 上面传入的request对象,后一个参数是一个字典
    # context.push(locals())#
    # 3. 模板渲染,产生标准的html内容
    context = {'goods': 'goods'}  # context必须是一个字典类型
    res_html = temp.render(context)  # 生成渲染的内容
    # 4.返回给浏览器
    return HttpResponse(res_html)

这里context必须是一个字典类型的值,不然会报错:
TypeError at /index
context must be a dict rather than RequestContext.

模块化
def myrender(request, template_path, context_dict={}):  # 定义context_dict默认值为空,可以不传入参数

    temp = loader.get_template(template_path)
    context = RequestContext(request,context_dict)
    context = context_dict
    res_html = temp.render(context)
    return HttpResponse(res_html)

# http://127.0.0.1:8000/index
def index(request):
    return HttpResponse(myrender(request, 'goods/index.html'))
使用模板变量{{变量名}}

在index.html中添加以下语句

<li>使用模板变量variable</li>
<ol>{{variable}}</ol>

为view.py中的index视图函数传入变量

def index(request):
    return HttpResponse(myrender(request, 'goods/index.html',{'variable': 123}))

结果;
在这里插入图片描述

在模板中嵌入循环语句{% 代码段%}
<li>模板中嵌套循环</li>
{% for i in list %}
<ol>
    {{ i }}
</ol>
{% endfor%}
# http://127.0.0.1:8000/index
def index(request):
        return HttpResponse(myrender(request, 'goods/index.html',{'list': [1, 2, 3, 4],'variable': 123}))

结果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值