美文网首页
django篇模板

django篇模板

作者: Lee_M | 来源:发表于2017-11-09 18:52 被阅读3次

    使用模板步骤

    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

    相关文章

      网友评论

          本文标题:django篇模板

          本文链接:https://www.haomeiwen.com/subject/uqemmxtx.html