Django联系人表单类型错误:上下文必须是dict而不是Contex

2024-10-02 00:22:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我遇到了一个错误“上下文必须是dict而不是context”。在提交一个联系人表单时。我有预感,这是由于与django1.11的不兼容问题造成的。不太清楚如何找到解决办法。在

以下是我在回溯中得到的信息:

http://dpaste.com/18T2D2V

Environment:

Request Method: POST
Request URL: http://127.0.0.1:8000/contact/

Django Version: 1.11.3
Python Version: 3.6.0
Installed Applications:
['collection',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.humanize',
 'registration']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']

Traceback:

File "/Users/billphan/Desktop/Projects/hello-web-app/venv/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
  41.             response = get_response(request)

File "/Users/billphan/Desktop/Projects/hello-web-app/venv/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "/Users/billphan/Desktop/Projects/hello-web-app/venv/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/Users/billphan/Desktop/Projects/hello-web-app/collection/views.py" in contact
  95.             content = template.render(context)

File "/Users/billphan/Desktop/Projects/hello-web-app/venv/lib/python3.6/site-packages/django/template/backends/django.py" in render
  64.         context = make_context(context, request, autoescape=self.backend.engine.autoescape)

File "/Users/billphan/Desktop/Projects/hello-web-app/venv/lib/python3.6/site-packages/django/template/context.py" in make_context
  287.         raise TypeError('context must be a dict rather than %s.' % context.__class__.__name__)

Exception Type: TypeError at /contact/
Exception Value: context must be a dict rather than Context.

这是我在视图.py文件:

^{pr2}$

这显然是导致错误的那条线:

content = template.render(context)

不太确定如何解决这个问题,寻求一些指导!谢谢!在


Tags: djangoinpywebapphelloresponsecontext
2条回答

创建上下文时,只需创建字典,而不是Context对象。在

context = {
            'contact_name': contact_name,
            'contact_email': contact_email,
            'form_content': form_content,
          }

尝试更换

context = Context({
    'contact_name': contact_name,
    'contact_email': contact_email,
    'form_content': form_content,
})

^{pr2}$

相关问题 更多 >

    热门问题