django.template.exceptions.TemplateDoesNotExist: blog/index.html
来源:互联网 发布:宾夕法尼亚大学 知乎 编辑:程序博客网 时间:2024/05/22 04:24
django.template.exceptions.TemplateDoesNotExist: blog/index.html
错误原因:错把TEMPLATE写成TEMPLATE_DIRS
TEMPLATE_DIRS = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join(My_Blog, 'templates').replace('\\', '/'),], 'APP_DIRS': True, # 如果'APP_DIRS': True,则直接在app的目录下寻找templates,这个是默认设置,所以最简单的是直接将templates文件夹放在app下。 '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', ], }, },]
TEMPLATES = [# 整个setting只有此处不同。 { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [], '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', ], }, },]
解决办法: TEMPLATE_DIRS是Django 1.9 以前的语法,再1.9以后就不再支持了。
其他基础原理:
常见的习惯是载入一个模板、填充一个context 然后返回一个含有模板渲染结果的HttpResponse对象
template = loader.get_template('polls/index.html') context = RequestContext(request, { 'latest_question_list': latest_question_list, }) return HttpResponse(template.render(context))
为了方便书写,我们有一种更快捷的函数等你使用。
context = {'latest_question_list': latest_question_list} return render(request, 'polls/index.html', context)
那问题来了,render和httpResponse俩产生的结果是一样的吧?
render()函数将请求对象作为它的第一个参数,模板的名字作为它的第二个参数,一个字典作为它可选的第三个参数。 它返回一个HttpResponse对象,含有用给定的context 渲染后的模板。
系统是如何根据’polls/index.html’找到模板的?
Step1: create a directory called templates in your blog(我的app名是blog) directory.
Step2:Within the templates directory you have just created ,create another directory called blog,within that create a file called index.html.(大家都是这么做的,所以就约定俗成了。这时候你的html应该在 blog/templates/blog/index.html)
Step3: Django will look for templates in there.
阅读全文
0 0
- django.template.exceptions.TemplateDoesNotExist: blog/index.html
- Django报错:django.template.exceptions.TemplateDoesNotExist: index.html
- django 1.11 报表:django.template.exceptions.TemplateDoesNotExist:
- django.template.base.TemplateDoesNotExist: test.html
- Django TemplateDoesNotExist
- 解决django TemplateDoesNotExist错误
- django TemplateDoesNotExist at /
- python3 Django TemplateDoesNotExist at /
- Django 异常TemplateDoesNotExist
- django:TemplateDoesNotExist错误的解决方法
- django 1.8 出现 TemplateDoesNotExist 错误
- django 1.8 出现 TemplateDoesNotExist 错误
- Django出错提示TemplateDoesNotExist at /
- TemplateDoesNotExist
- django 模板找不到TemplateDoesNotExist报错!
- 关于Django模板报TemplateDoesNotExist异常
- 技术小黑屋blog http://droidyue.com/blog/archives/index.html
- Django template
- Hive安装配置指北(含Hive Metastore详解)
- Python 使用Gmail发送邮件
- RTDT 基于websocket的数据传输框架
- 四元数
- 映射关系小技巧
- django.template.exceptions.TemplateDoesNotExist: blog/index.html
- 关于RTP时间戳及多媒体通信同步的问题/H264关于RTP协议的实现
- 基于信号处理的在线云评测+社区系统( 9)
- 【总结】常见Java故障案例
- Spark Executor 报错 java.lang.StackOverflowError
- 优化器、激活函数、评价函数
- 如何判断设备是网站以及移动设备
- MySQL-基本使用
- OSGi主要应用场景 和 发展前景