Django报错TemplateDoesNotExist at /login/ (Source Does Not Exist) 求助
Hey there! Let's work through this frustrating TemplateDoesNotExist error you're facing while adding a login page to your Django polls app. Since you mentioned Django is looking in the right directory but still says "Source doesn't exist", here are the most likely fixes to try:
1. Double-Check Filename & Path Exactness
Typos and case sensitivity are the #1 culprits here:
- Make sure your template file is exactly named
login.html(no capital letters likeLogin.html—Linux/macOS systems are case-sensitive, and even Windows can sometimes trip up on this). - Verify the file is in the correct subdirectory. For standard app-level templates, it should live at:
The innerpolls/ └── templates/ └── polls/ └── login.htmlpollsfolder inside templates is key—Django looks for templates inapp_name/templates/app_name/whenAPP_DIRS=Trueis enabled.
2. Validate Your Template Settings in settings.py
Open your project's settings.py and check the TEMPLATES configuration block:
- Ensure
APP_DIRSis set toTrue:TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [], # Leave empty for app-level templates; add project template paths here if needed 'APP_DIRS': True, # ... other default settings }, ] - If you're using a project-wide
templatesdirectory, confirm the path is correctly added toDIRS:
And make sure'DIRS': [BASE_DIR / 'templates'],login.htmlis directly inside that projecttemplatesfolder.
3. Check Your Login View Configuration in urls.py
If you're using Django's built-in LoginView, ensure you're specifying the correct template_name:
For example, in your secvot/urls.py (or polls app's urls.py):
from django.contrib.auth import views as auth_views urlpatterns = [ # ... other paths path('login/', auth_views.LoginView.as_view(template_name='polls/login.html'), name='login'), ]
The template_name value must match the exact path Django expects (either polls/login.html for app-level templates, or just login.html for project-level).
4. Clear Django's Template Cache
Sometimes Django caches template location data, even after you fix the path. Run this command to clear the cache, then restart your dev server:
python manage.py clearcache
5. Verify File Permissions (Linux/macOS)
If you're on a Unix-based system, make sure the login.html file has read permissions for the user running the Django dev server. You can fix this with:
chmod 644 polls/templates/polls/login.html
Example Error Context
To align with what you're seeing, this is the kind of error log Django would output when it finds the right directory but can't locate the file:
TemplateDoesNotExist at /login/
polls/login.html
Django tried loading these templates, in this order:
'polls/login.html' (source doesn't exist)
...
If you've tried all these steps and still hit the error, feel free to share snippets of your settings.py TEMPLATES block, your urls.py login path, and the exact file structure of your templates folder—we can dig deeper!
内容的提问来源于stack exchange,提问作者K str

