繁体   English   中英

Django Contact Form TypeError:上下文必须是字典而不是Context

[英]Django Contact Form TypeError: context must be a dict rather than Context

我遇到一个错误“上下文必须是字典而不是上下文”。 提交联系表后。 我预感这是由于Django 1.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.

这是我的views.py文件中联系路线的代码段:

def contact(request):
form_class = ContactForm

# new logic!
if request.method == 'POST':
    form = form_class(data=request.POST)

    if form.is_valid():
        contact_name = form.cleaned_data['contact_name']
        contact_email = form.cleaned_data['contact_email']
        form_content = form.cleaned_data['content']

        # email the profile with the contact info
        template = get_template('contact_template.txt')

        context = Context({
            'contact_name': contact_name,
            'contact_email': contact_email,
            'form_content': form_content,
        })
        content = template.render(context)

        email = EmailMessage(
            'New contact form submission',
            content,
            'Your website <test@gmail.com>',
            ['youremail@gmail.com'],
            headers = {'Reply-To': contact_email }
        )
        email.send()
        return redirect('contact')

return render(request, 'contact.html', {
    'form': form_class,
})

显然,这是导致错误的行:

content = template.render(context)

不太确定如何解决此问题,请寻求指导! 谢谢!

尝试更换

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

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,
          }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM