问题描述:
在使用django框架进行web开发的时候,views文件中return到模板文件中的某个html文件,但是显示的是template file not found 。而我在settings中的TEMPLATES的DIRS已经加入了template 路径。
代码如下:
settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
"myApp",
]
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',
],
},
},
]
views.py
def get_res(request):
return render(request, "myApp/showres.html"),
原因分析:
寻找了N久,大概分析原因,pycharm并没有将你的templates文件当作模板文件,也就是说render的第二个参数的寻找范围不包含templates
解决方案:
我们将templates文件夹设置称为解析器默认的模板文件目录即可
1.鼠标右键你的模板文件夹
2.选择Mark Directory as(我的pycharm是倒数第三个)
3.选择Template Floder
选择Template Floder后我的Templates颜色变成了紫色
views.py文件不再显示not found