Django 2.0.2开发Web应用遇ImportError求助:myapp.templates无hello属性/类
Hey there! Let's break down this ImportError you're hitting—this is a super common gotcha when you're starting out with Django templates. The error is telling you that you're trying to treat a template file or directory like a Python module, which doesn't work the way you might think.
First, Let's Diagnose the Root Cause
Chances are, in your views.py, you've got something like this (the wrong approach):
# ❌ Wrong! Don't import templates like Python modules from myapp.templates import hello
Templates aren't Python modules—they're HTML (or template language) files that Django renders via its template engine, not imported directly. Let's fix this step by step.
Step 1: Correct Your views.py
Use Django's built-in render() shortcut (the recommended way) to load and render your template:
# ✅ Correct approach using render() from django.shortcuts import render def hello_view(request): # Django will automatically look in myapp/templates/ for hello.html return render(request, 'hello.html')
If you need more control (like manually loading the template), you can use the Template class instead:
# ✅ Alternative manual approach from django.template import Template, Context from django.http import HttpResponse def hello_view(request): # Load the template file directly with open('myapp/templates/hello.html', 'r') as f: template_content = f.read() template = Template(template_content) context = Context({'greeting': 'Hello there!'}) # Pass variables to the template return HttpResponse(template.render(context))
Step 2: Verify Your urls.py
Make sure your URL pattern points to the correct view function:
from django.urls import path from . import views urlpatterns = [ path('hello/', views.hello_view, name='hello'), # Maps /hello/ to your view ]
Step 3: Check settings.py Template Configuration
Ensure Django knows to look for templates inside your app. In settings.py, confirm the TEMPLATES section has APP_DIRS set to True:
TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [], # You can add global template paths here if needed 'APP_DIRS': True, # Critical—tells Django to check app-specific templates folders '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', ], }, }, ]
Also, don't forget to add myapp to INSTALLED_APPS if you haven't already:
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'myapp', # Add your app here ]
Step 4: Confirm Your Folder Structure
Double-check that your templates are in the right place (Django expects this structure by default):
your_project_root/ ├── myapp/ │ ├── templates/ │ │ └── hello.html # Your template file lives here │ ├── views.py │ ├── urls.py │ └── apps.py ├── your_project/ │ ├── settings.py │ └── urls.py
Why This Works
Django's template engine is designed to find and render template files without you needing to import them as Python modules. When APP_DIRS is True, Django automatically scans each installed app's templates directory for the file you specify in render().
Give these changes a try, and your ImportError should vanish!
内容的提问来源于stack exchange,提问作者Ssukumar

