django篇模板

2017-11-09  本文已影响3人  Lee_M

使用模板步骤

1、一般新建模板文件夹,在模板文件夹中新建app一样的文件夹,里面在创建html


image.png

2、在项目的setting中指定模板位置

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',
            ],
        },
    },
]

image.png

3、在app的视图中,链接模板

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.http import *

from  django.template import RequestContext,loader #这个类是加载模板

from django.shortcuts import render

# Create your views here.
def index(request): #定义视图
    #temp = loader.get_template('booktest/index.html')
    #return HttpResponse(temp.render())
    return render(request,'booktest/index.html')
image.png

效果


image.png
上一篇下一篇

猜你喜欢

热点阅读