You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django报错TemplateDoesNotExist at /login/ (Source Does Not Exist) 求助

Troubleshooting TemplateDoesNotExist for Django Polls App Login Page

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 like Login.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:
    polls/
    └── templates/
        └── polls/
            └── login.html
    
    The inner polls folder inside templates is key—Django looks for templates in app_name/templates/app_name/ when APP_DIRS=True is enabled.

2. Validate Your Template Settings in settings.py

Open your project's settings.py and check the TEMPLATES configuration block:

  • Ensure APP_DIRS is set to True:
    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 templates directory, confirm the path is correctly added to DIRS:
    'DIRS': [BASE_DIR / 'templates'],
    
    And make sure login.html is directly inside that project templates folder.

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 07:04:56