python_django_views文件中return render("XXXX/XXX.html") 显示 Template file not found

问题描述:
在使用django框架进行web开发的时候,views文件中return到模板文件中的某个html文件,但是显示的是template file not found 。而我在settings中的TEMPLATES的DIRS已经加入了template 路径。

代码如下:

settings.py

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

return render(request, 'account/login.html', {'form': form})


原因分析:
寻找了N久,大概分析原因,pycharm并没有将你的templates文件当作模板文件,也就是说render的第二个参数的寻找范围不包含templates

解决方案:
我们将templates文件夹设置称为解析器默认的模板文件目录即可

1.鼠标右键你的模板文件夹

2.选择Mark Directory as(我的pycharm是倒数第三个)

3.选择Template Floder

选择Template Floder后我的Templates颜色变成了紫色

 

views.py文件不再显示not found

猜你喜欢

转载自www.cnblogs.com/tuobei/p/12339677.html