按照最新版,一步一步做,但是最后发现模板不存在。书里面建立的 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