django-模板之自定义模板路径一

一般情况下我们的模板路径是位于app下的templates,我们可以根据实际情况自己定义模板的路径。

我们在与app的同级目录下建立一个templates,并在settings.py中进行路径配置。

基本目录如下:

 

 我们现在有book/templates/index.py和/templates/index.py

在book/views.py中有

from django.views import View
from django.shortcuts  render
# Create your views here.
class IndexView(View):
    def get(self,request):
        return render(request,"index.html")

在settings.py中,有

 

TEMPLATES = [
    {
        'BACKEND': django.template.backends.django.DjangoTemplates',#当前app目录
        DIRS: [],
     #是否包含app目录
APP_DIRS: True,OPTIONS: { context_processors: [ django.template.context_processors.debugdjango.template.context_processors.requestdjango.contrib.auth.context_processors.authdjango.contrib.messages.context_processors.messages

 

 修改之后:

 

 会调用我们指定目录下的index.py

 

 另一种方式,修改回去:

 

 

相关文章

注:所有源代码均实测运行过。所有源代码均已上传CSDN,请有...
继承APIView和ViewSetMixin;作用也与APIView基本类似,提供...
一、Django介绍Python下有许多款不同的 Web 框架。Django是重...
本文从nginx快速掌握到使用,gunicorn快速掌握到使用,实现小...
uniapp微信小程序订阅消息发送服务通知
Django终端打印SQL语句 1 Setting配置: 2 默认python 使用的...