Framework Error
TemplateNotFound at /accounts/login: Unable to find template 'login.html' in the templates directory.
What This Error Means
This error occurs when the Flask or Django framework is unable to locate a specific HTML template. It's often caused by a typo in the template name, a misspelled directory path, or a missing template file.
Why It Happens
The error happens when the framework's template loader is unable to find the requested template. This can be due to a variety of reasons such as a typo in the template name, a misspelled directory path, or a missing template file. In Django, it can also be caused by the TEMPLATE_DIRS setting being misconfigured.
How to Fix It
- 1To fix this error, ensure that the template file exists in the correct directory and that the directory path is correct. In Flask, you can use the 'flask' command to find the correct template path. In Django, you can use the 'django-admin startproject' command to create a new project and then inspect the TEMPLATE_DIRS setting.
Example Code Solution
from flask import Flask, render_template
app = Flask(__name__)
app.config['TEMPLATES_DIR'] = '/templates'
@app.route('/accounts/login')
def login():
return render_template('wrong_login.html') # incorrect template namefrom flask import Flask, render_template
app = Flask(__name__)
app.config['TEMPLATES_DIR'] = '/templates'
@app.route('/accounts/login')
def login():
return render_template('login.html') # correct template nameFix for TemplateNotFound at /accounts/login: Unable to find template 'login.html' in the templates directory.
Browse Related Clusters
Related PYTHON Errors
Related PYTHON Blog Articles
Have a different error? Get an instant explanation.
Explain Another Error