请教一个 tango with django 1.9 版本的 templates 路径问题,感激不尽

2017-06-22 11:49:29 +08:00
 garygay
按照最新版,一步一步做,但是最后发现模板不存在。书里面建立的 templates 位置和官方不太一样,需要配置,但是我打印路径是对的,很奇怪。rango 是 APP 的名字,tango_with_django_project 是项目名字。

├─rango
│ │ admin.py
│ │ apps.py
│ │ models.py
│ │ tests.py
│ │ urls.py
│ │ views.py
│ │ __init__.py
│ │
│ ├─migrations
│ │ │ __init__.py



└─tango_with_django_project
settings.py
urls.py
wsgi.py
__init__.py

├─templates
│ └─rango
│ index.html

settings 根据文档是这么配置的
TEMPLATE_PATH = os.path.join(BASE_DIR, 'templates')
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [TEMPLATE_PATH],
'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_PATH 路径如下:
C:\Users\GARRY\myenv\tango_with_django_project\templates
和创建目录路径是符合的

views.py 里是这么写的:
def index(request):
context_dict = {'boldmessage': 'Tango with Django,hahaha'}
return render(request, 'rango/index.html', context=context_dict)

最后报错,说找不到
TemplateDoesNotExist at /
rango/index.html
Request Method: GET
Request URL: http://localhost:8000/
Django Version: 1.11.2
Exception Type: TemplateDoesNotExist
Exception Value: rango/index.html

看下了国外类似问题,与我写的一样,所以最后求救各位。谢谢
https://stackoverflow.com/questions/29987619/templatedoesnotexist-at-rango
2051 次点击
所在节点    Python
4 条回复
garygay
2017-06-22 11:52:43 +08:00
树图看不清楚,templates 是建立在项目 tango_with_django_project 下的,然后下面是 rango-index.html
garygay
2017-06-22 12:19:55 +08:00
问题找到了 ,是配置路径的问题 。谢谢大家。
garygay
2017-06-22 12:27:51 +08:00
最后发现可能是版本问题,还是按照官方最新的 1.11 的,把 templates 建立在 APP 目录下。
zsdostar
2017-06-22 12:28:32 +08:00
可以试试用继承 View 类的类来写 view, 这种
from django.views.generic.base import View

class index(View):
def get(self, request):
context_dict = {'boldmessage': 'Tango with Django,hahaha'}
return render(request, 'rango/index.html', context=context_dict)

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/370265

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX