【问题标题】:TypeError: 'module' object is not callable Django 3 render functionTypeError:“模块”对象不可调用 Django 3 渲染函数
【发布时间】: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


【解决方案1】:

django.contrib.sessions 不是可调用对象,因此它不是有效的上下文处理器。

事实上,它是一个应用程序,因此,它应该在您的 INSTALLED_APPS 列表中,而不是在 TEMPALTES context_processors 列表中。从那里删除它应该可以解决此问题。


为什么会这样?

异常提到它发生在 django/template/context.py 第 246 行(在 Django v3.0.11 中)。如果您see the source code at line 246,您可以看到在这一行 Django 正在运行已注册的模板上下文处理器。因为,django.contrib.sessions 不是可调用对象,而是模块,所以您会收到以下异常消息:'module' object is not callable

【讨论】:

  • 哇。有效!非常感谢您向我解释错误。我没有发生什么事。另外,也许我把 django.contrib.sessions 弄错了。你为我节省了很多时间。谢谢!
猜你喜欢
  • 2021-12-24
  • 1970-01-01
  • 1970-01-01
  • 2017-06-17
  • 2014-09-21
  • 2011-05-30
  • 2021-11-22
相关资源
最近更新 更多