gpt4 book ai didi

python - Django 联系表单类型错误 : context must be a dict rather than Context

转载 作者:太空宇宙 更新时间:2023-11-04 00:31:48 33 4
gpt4 key购买 nike

我遇到错误“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,
}

关于python - Django 联系表单类型错误 : context must be a dict rather than Context,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45539499/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com