首先在settings.py文件中配置:
网站建设哪家好,找成都创新互联!专注于网页设计、网站建设、微信开发、小程序定制开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了荆州免费建站欢迎大家使用!
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',
],
},
},
]
BACKEND:模板引擎类的python路径,内置的模板引擎分别有'django.template.backends.django.DjangoTemplates'和'django.template.backends.jinja2.Jinja2'
DIRS:模板引擎搜索模板的路径,如上,默认搜索project目录下的templates目录
APP_DIRS:告诉模板引擎是否搜索app目录下的templates目录。默认为true,即是默认搜索app目录下的templates目录
'DIRS': [os.path.join(BASE_DIR, 'templates')] 是指到 BASE_DIR/templates文件夹中去取模板
'DIRS': [os.path.join(BASE_DIR, 'app1/templates')] 是指到 BASE_DIR/app1/templates文件夹中去取模板
模板文件查找顺序
如果同时设置
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
并且同时存在
mysite\templates\polls\index.html
mysite\polls\templates\polls\index.html
则会访问 mysite\templates\polls\index.html 而不是 mysite\polls\templates\polls\index.html
模板文件的命名空间(其实质是模板文件的本地相对路径)的应用场景是,在每个APP下建立appname/templates/appname/,然后在该目录下再放模板文件(index.html)。然后在需要引用模板文件的地方,采用模板文件相对地址硬编码时,形式如下:appname/index.html.
例如在views.py中
def index(request):
#return HttpResponse("Hello, world. You're at the polls index.")
return render(request,'polls/index.html')
或者index.html中
<li><ahref="/polls/xxx.html">{{question.question_text}}</a></li>
这根URL命名空间不一样:URL命名空间是用于模板文件中对URL(所谓URL是浏览器访问时的网络地址)的反向引用,一般会使用模板标签{% url %}
例如在index.html中
{% url 'polls:detail' question.id %}"
分享名称:django模板文件路径设置
分享网址:https://www.cdcxhl.com/article42/ijdghc.html
成都网站建设公司_创新互联,为您提供静态网站、企业建站、定制网站、品牌网站制作、网站排名、建站公司
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联