Django模板配置语法错误与模板文件不存在问题求助
Got it, let's work through these two Django template issues you're facing—they're both common fixes once you spot the root causes!
1. Fixing the SyntaxError in settings.py TEMPLATES Configuration
That syntax error on line 59 ('APP_DIRS': True,) is caused by a mismatched bracket in your DIRS configuration. Look closely at your code: you added an extra opening bracket and forgot to close the list properly, which breaks the entire dictionary structure Python is trying to parse.
Problematic Code Snippet:
'DIRS': [[os.path.join(BASE_DIR,'templates')], # Extra [ and missing closing ]
Corrected TEMPLATES Configuration:
TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', # Single bracket for the DIRS list, properly closed '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', ], }, }, ]
The DIRS setting expects a list of file paths—so only one set of square brackets is needed, and we make sure to close the list with a ] before moving to the next configuration key (APP_DIRS).
2. Resolving the "Template Not Found" Error
If your browser says the template file doesn't exist even after creating it, let's check the most common culprits one by one:
a. Make Sure Your App is Registered in settings.py
Django only looks for templates in apps that are added to INSTALLED_APPS. Open your settings.py and confirm newapp is listed:
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'newapp', # Add this line if it's missing! ]
b. Verify Your Template File Structure
Since you have APP_DIRS: True enabled, Django will automatically check for a templates folder inside your registered apps. To avoid template name conflicts (and ensure Django finds it easily), structure your app like this:
newapp/ apps.py # Your existing NewappConfig file templates/ newapp/ # Create a subfolder with your app's name hello.html # Your template with "hello there!!!" # Other app files (views.py, models.py, etc.)
The nested newapp folder under templates is a best practice—Django will look for newapp/hello.html when you reference it in your view.
c. Check Your View's render() Call
In your newapp/views.py, make sure you're referencing the template correctly:
from django.shortcuts import render def hello_view(request): # Match the template path we set up above return render(request, 'newapp/hello.html')
If you placed the template directly in the project root's templates folder (the one you configured in DIRS), you'd use 'hello.html' instead—but the app-specific folder is better practice for scalability.
d. Confirm Your URL Mapping is Correct
Finally, make sure your URL routes point to the right view. For example:
- In your project's root
urls.py:from django.contrib import admin from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), path('', include('newapp.urls')), # Link to your app's URLs ] - In
newapp/urls.py(create this file if it doesn't exist):from django.urls import path from . import views urlpatterns = [ path('', views.hello_view, name='hello'), ]
内容的提问来源于stack exchange,提问作者Ankita kandharkar

