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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM