慕课网-Django入门到进阶-更适合Python小白的系统课程-第3章Django中的Template模板-3.1模板的配置绑定与渲染

第3章 Django 中的 Template 模板

3.1 模板的配置绑定与渲染

Template 模板

模板可以动态生成 Html 网页,它包括部分 Html 代码和一些特殊的语法

Template 配置方法

1.一般 Template 模板存放在 "templates" 目录中

2.通过在项目 Settings 的 templates 的 DIRS 列表中添加对应的路径即可,如:os.path.join(BASE_DIR, 'templates')

Template 与视图的绑定

1.通过 from django.shortcuts import render 模块

2.return render(request, template_path, {k:v}) 字典中的 key 和 value 就是要向前端渲染出的数据

Template 展示渲染的数据

在 html 中以 {{}} 为标识,在双大括号中传入视图中传入的数据

创建项目

django-admin startproject template
cd template
python manage.py startapp app

修改项目

1.在项目下添加目录 templates

2.进入项目下目录 template,修改文件 settings.py,修改编码、时区和注册应用,添加 template 路径

#注册应用
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'app',
]
 
#修改编码
LANGUAGE_CODE = 'zh-hans'
 
#修改时区
TIME_ZONE = 'asia/Shanghai'

#添加 template 路径
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        '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',
            ],
        },
    },
]

3.进入项目下目录 app,添加文件 urls.py,添加应用的路由

#!/usr/bin/env python
# -*- coding:utf-8 -*-
from django.urls import path
from .views import Index

urlpatterns = [
    path('', Index.as_view(), name='index')
]

4.进入项目下目录 app,修改文件 views.py,添加一个 Index 类

#coding:utf-8
from django.shortcuts import render
from django.views.generic import View

class Index(View):
    TEMPLATE = 'index.html'

    def get(self, request):
        return render(request, self.TEMPLATE, {'name':'dewei'})

5.进入项目下目录 template,修改文件 urls.py,添加 app 应用的路由

from django.contrib import admin
from django.urls import path, include
from app import urls as app_urls

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include(app_urls)),
]

6.进入项目下目录 templates,添加文件 index.html

7.进入项目下目录 templates,修改文件 index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>hello {{name}}</h1>
</body>
</html>

8.测试

9.进入项目下目录 app,修改文件 urls.py,添加参数

#!/usr/bin/env python
# -*- coding:utf-8 -*-
from django.urls import path
from .views import Index

urlpatterns = [
    path('<str:name>', Index.as_view(), name='index')
]

10.进入项目下目录 app,修改文件 views.py,添加参数

#coding:utf-8
from django.shortcuts import render
from django.views.generic import View

class Index(View):
    TEMPLATE = 'index.html'

    def get(self, request, name):
        return render(request, self.TEMPLATE, {'name':name})

11.测试

posted on 2020-01-16 05:16  herisson_pan  阅读(8)  评论(0)    收藏  举报

导航