Django3中使用include引入URL配置后模板无法显示的问题排查
TemplateDoesNotExist Error for Your Dashboard App Hey there, let's work through this template issue you're facing with your Django dashboard app—we've all been stuck on template path problems when starting out, so let's break down the fixes step by step:
1. Update the Template Path in Your View
Right now, your view is looking directly for dashboard.html, but your template lives inside dashboard/templates/dashboard/dashboard.html. Django searches for templates within each app's templates folder, so you need to include the subfolder name in the path.
Modify your dashboard view code to this:
from django.shortcuts import render # Create your views here. def dashboard(request): home="dashboard" # Use the full relative path inside the app's templates folder return render(request, 'dashboard/dashboard.html', {'home':home})
2. Confirm Your Dashboard App is in INSTALLED_APPS
Django only looks for templates in apps that are listed in your project's settings.py under INSTALLED_APPS. Double-check that you've added dashboard there:
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', # Make sure this line exists 'dashboard', ]
3. Verify Your Folder Structure is Exact
Double-check that your template folder structure matches this exactly (pay attention to folder names and nesting):
your-project-root/ ├─ dashboard/ │ ├─ templates/ │ │ ├─ dashboard/ │ │ │ └─ dashboard.html # Your template file lives here │ ├─ views.py │ └─ urls.py
4. Check for Typos or Case Sensitivity
- Make sure
dashboard.htmlis spelled correctly (no typos likedashbaord.html). - On Linux/macOS, filenames are case-sensitive—if your file is named
Dashboard.htmlinstead ofdashboard.html, Django won't find it.
Why base.html Loaded but Was Empty
When you switched to base.html, Django was likely finding a base.html file in your project's root templates folder (if you have one), but that file probably has no content or doesn't include the template blocks you need. This confirms that Django's template system is working—you just had the wrong path for your dashboard template.
Try these steps one by one, and your dashboard template should load properly. If you run into any other issues, feel free to share more details!
内容的提问来源于stack exchange,提问作者Nauman K

