Django模板继承extends失效问题求助
Hey there! I get it—frustrating when you’re following along with Django by Example, copying the exact code, and the template language still won’t behave. Let’s walk through the most common fixes for your blog app scenario:
1. Verify Your App is Registered & Template Settings Are Correct
First, make sure your blog app is added to INSTALLED_APPS in settings.py—Django won’t look for templates in unregistered apps:
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'blog', # Add this line if it's missing! ]
Then check your TEMPLATES configuration. For app-specific templates, APP_DIRS should be True (this tells Django to scan each app’s templates folder):
TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [], # Leave empty if using app-level templates, or add project-wide paths here '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', ], }, }, ]
2. Double-Check Template Inheritance Syntax
Your base template lives at blog/templates/blog/base.html, so your post list template (in blog/templates/blog/post/) needs to extend it correctly. Make sure the first line of your list template is:
{% extends "blog/base.html" %}
If you misspell the path (e.g., base.html instead of blog/base.html) or use the wrong directory structure, Django won’t pick up the base template, and your blocks won’t render.
Also, confirm your base template defines blocks that the child template overrides. For example, in base.html:
{% block content %}{% endblock %}
And in your list template:
{% block content %} {% for post in posts %} <h2>{{ post.title }}</h2> <p>{{ post.body }}</p> {% endfor %} {% endblock %}
Typos in block names (like content vs contect) will break inheritance too.
3. Ensure Views Are Passing Correct Context Data
If template variables (like {{ post.title }}) aren’t rendering, your view might not be sending the right data to the template.
For Function-Based Views:
Make sure you’re passing the context dictionary to render():
from django.shortcuts import render from .models import Post def post_list(request): posts = Post.objects.all() # Fetch your Post objects return render(request, 'blog/post/list.html', {'posts': posts}) # 'posts' matches template variable
For Class-Based Views (like ListView):
Confirm you’ve set the correct model, template path, and context variable name:
from django.views.generic import ListView from .models import Post class PostListView(ListView): model = Post template_name = 'blog/post/list.html' context_object_name = 'posts' # Ensures the template uses 'posts' instead of the default 'object_list'
If you skip context_object_name, you’ll need to use {{ object_list }} in your template instead of {{ posts }}.
4. Check for Template Syntax Errors
Small typos are easy to miss:
- Use double curly braces for variables:
{{ post.title }}(not{ post.title }or{{{ post.title }}}) - Use
{% ... %}for template tags (like{% extends %},{% for %},{% block %}) - Make sure all tags are properly closed (e.g.,
{% endfor %}to match{% for %},{% endblock %}for blocks)
5. Clear Caches & Restart the Server
- Sometimes your browser caches old, unrendered HTML—force a hard refresh with
Ctrl+F5(Windows/Linux) orCmd+Shift+R(Mac). - Restart your Django development server to ensure all code changes are loaded.
6. Use Debug Mode to Spot Errors
Keep DEBUG = True in settings.py—Django will show detailed error pages if something’s wrong (like a missing template, invalid variable name, or syntax error). These error messages are your best friend for pinpointing issues!
内容的提问来源于stack exchange,提问作者Mahdi-Jafaree

