我遇到错误"上下文必须是dict而不是Context。"在提交联系表格时。由于与Django 1.11的不兼容问题,我有一种预感。不太确定如何找到解决方法。
以下是我在追溯上的内容:
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)
不太确定如何解决这个问题,寻求一些指导!谢谢!
答案 0 :(得分:3)
尝试替换
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,
}
答案 1 :(得分:0)
创建上下文时,只需创建一个字典,而不是max_queue_size
对象。
Context