【发布时间】:2021-01-14 18:37:44
【问题描述】:
我只是在我的 Django 3 应用程序中创建一个简单的 hello world 页面,但出现错误
TypeError: 'module' object is not callable
这是错误
TypeError at /
'module' object is not callable
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 3.0.11
Exception Type: TypeError
Exception Value:
'module' object is not callable
Exception Location: C:\Users\admin\AppData\Roaming\Python\Python37\site- packages\django\template\context.py in bind_template, line 246
Python Executable: C:\Program Files (x86)\Microsoft Visual
Studio\Shared\Python37_64\python.exe
Python Version: 3.7.8
Python Path:
['C:\\Users\\admin\\Repositories\\django-docker\\django-portal-base\\app',
'C:\\Program Files (x86)\\Microsoft Visual '
'Studio\\Shared\\Python37_64\\python37.zip',
'C:\\Program Files (x86)\\Microsoft Visual
Studio\\Shared\\Python37_64\\DLLs',
'C:\\Program Files (x86)\\Microsoft Visual Studio\\Shared\\Python37_64\\lib',
'C:\\Program Files (x86)\\Microsoft Visual Studio\\Shared\\Python37_64',
'C:\\Users\\admin\\AppData\\Roaming\\Python\\Python37\\site-packages',
'C:\\Program Files (x86)\\Microsoft Visual '
'Studio\\Shared\\Python37_64\\lib\\site-packages']
Server time: Thu, 14 Jan 2021 19:15:32 +0000
它以前可以工作,但现在突然不工作了。这是我的views.py
from django.shortcuts import render
# from django.http import HttpResponse
# from django.template import RequestContext, loader
# from django.template import Context
def index(request):
"""Placeholder index view"""
print('XXXX')
return render(request, 'hello_world/index.html')
#return HttpResponse('Hello, World!')
def test(request):
context = {'foo': 'bar'}
return render(request, 'hello_world/index.html', context)
错误在return render(request, 'hello_world/index.html') 行,但是当我将其更改为return HttpResponse('Hello, World!') 时,它工作正常。
我的html文件很简单index.html
<h3> MY DJANGO APP</h3>
html文件也在正确的文件夹templates/hello_world/index.html
设置
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': False,
'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',
'django.contrib.sessions'
],
},
},
]
【问题讨论】:
-
渲染在定位
hello_world/index.html时遇到问题。您应该检查您的settings.py中的 TEMPLATES 变量,并查看路径是否与您的模板文件夹位置匹配。 -
@Johnson 谢谢,但我认为这与路径无关。这是正确的,如果我故意输入不正确的路径,错误将是 TemplateDoesNotExist,但事实并非如此
-
@jackhammer013 这可能是与模板上下文处理器有关的问题。 See Line 246,它运行模板上下文处理器。可能是因为上下文处理器不可调用。请发布您的
TEMPALTES设置。 -
@xyres 不确定我的设置是否有问题,但我发布了它。谢谢!
标签: python-3.x django django-3.0