1. Introduction to the problem
In Django4.1's official website tutorial - writing your first Django application, part 3, step by step, you will get an error:
There are also tips below:
2. Problem thinking
From the prompt, we know that the django-mysite (project name)/templates/polls/index.html and django-mysite (project name)/templates/polls/detail.html we wrote do not exist, because the python interpreter is installed in Python lib/site-packages/django/... under the path to search.
Obviously, our project was not created in the Python installation directory.
So the solution is much clearer: modify the directory where django retrieves index.html.
3. Looking for answers
In the following website, I found the answer, please see [Solution 6]
TemplateDoesNotExist in /polls/answers - Love Code Network (likecs.com)
4. Solve the problem
The specific operation is:
Modify the configuration of TEMPLATES (template) in django-mysite (project name)/mysite/settings.py so that the default address is the current project path.
That is, add these codes in the appropriate position:
import os
# 这个变量默认是没有的,建议在默认的TEMPLAETS变量前面一行定义
TEMPLATE_DIR = os.path.join(BASE_DIR,'templates')
# 然后在TEMPLATES变量中的'DIRS':[]的括号里,加上刚定义的变量,即
TEMPLATES = [
{
# 前面省略
'DIRS': [TEMPLATE_DIR],
# 后面省略
}
]
Then enter 127.0.0.1:8000/polls/ in the browser, no error will be reported