Django请求均导向子应用而非主应用,模板加载异常求助
Let's fix both of your problems step by step, starting with the root path template error.
Problem 1: TemplateDoesNotExist for index.html at Root Path
Why this happens
Looking at your settings.py TEMPLATES configuration, the DIRS list is empty, and APP_DIRS=True. When APP_DIRS is enabled, Django only searches for templates inside the templates directories of your installed apps—it doesn't check the project-root templates folder by default. Your template loader logs confirm this: it's only checking statistics101/templates, admin, and auth templates, not your project's root templates directory.
Fix
Update your settings.py to include the project-root templates folder in the DIRS list. First, make sure BASE_DIR is defined (it should be by default in Django projects), then modify the TEMPLATES section:
# kitchen_analytics/settings.py import os # This line should already exist in your settings BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', # Add the project-root templates directory here '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', ], }, }, ]
This tells Django to first look in your project-root templates folder before checking app-specific templates directories, so it'll find index.html correctly.
Also, note a potential issue in your urls.py: you have two empty path patterns:
path('', TemplateView.as_view(template_name='index.html')), path('', include(statistics101.urls, namespace='statistics101')),
Django matches URLs in order, so the first empty path will always trigger, and the statistics101 URLs will never be reached. If you want both to be accessible, give the statistics101 URLs a prefix, like:
path('stats/', include(statistics101.urls, namespace='statistics101')),
Problem 2: Statistics101 Templates Must Be in templates/statistics101
Why this is happening (and why it's actually a good thing)
This is Django's template namespace convention, designed to prevent template name collisions between apps. When APP_DIRS=True, Django scans all installed apps' templates folders. If you put orders_list.html directly in statistics101/templates, another app with a orders_list.html would cause Django to load the wrong one.
Option 1: Stick with the namespace (recommended)
Keep using statistics101/templates/statistics101/orders_list.html and reference it in your views like this:
# statistics101/views.py from django.shortcuts import render def orders_list(request): return render(request, 'statistics101/orders_list.html')
This guarantees no conflicts, even if you add more apps later.
Option 2: Place templates directly in statistics101/templates
If you really want to skip the namespace, you can—just make sure your template names are unique across all apps. For example, rename orders_list.html to statistics101_orders_list.html, or ensure no other app has a template with the same name. Then reference it directly:
return render(request, 'orders_list.html')
If you tried this and it didn't work, check these common issues:
- Typos in the template filename or the render call
- Another app has a template with the same name (Django will load the first one it finds)
statistics101isn't added toINSTALLED_APPSinsettings.py(double-check that line exists)
内容的提问来源于stack exchange,提问作者YKY

