我正在尝试使用Django cache
存储表单字段变量,并在我的视图中添加Django表单。
我收到此错误,我看不出我的脚本会出现什么问题:
Cannot convert dictionary update sequence element #0 to a sequence
你知道吗?从我的观点来看,一切似乎都是正确的。
错误来自这一行:
return render(request, 'form.html', {'form' : form})
这是我的脚本功能:
def Function_Form(request) :
query_lastname = request.GET.get('lastname')
cache.set('query_lastname', query_lastname, 300)
print cache.get('query_lastname')
if request.method == 'POST':
form = Formulary(request.POST or None)
if form.is_valid() :
if '_save' in request.POST :
post = form.save()
return HttpResponseRedirect(reverse('treated', kwargs={'id': post.id}))
else:
form = Formulary()
return render(request, 'form.html', {'form' : form})
编辑:
我们正在与@Valentin合作开展这个项目。我试图在这方面帮助他。
完整追溯:
Environment:
Request Method: GET
Request URL: http://localhost:8000/BirthCertificate/formulaire
Django Version: 1.10.3
Python Version: 2.7.12
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'BirthCertificate',
'Identity',
'bootstrapform',
'Accueil',
'django_countries',
'log',
'Mairie',
'Table',
'Recensement',
'Configurations',
'debug_toolbar']
Installed Middleware:
['django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.middleware.gzip.GZipMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
'debug_toolbar.middleware.DebugToolbarMiddleware']
Traceback:
File "/usr/local/lib/python2.7/site-packages/django/core/handlers/exception.py" in inner
39. response = get_response(request)
File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py" in _legacy_get_response
249. response = self._get_response(request)
File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python2.7/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
23. return view_func(request, *args, **kwargs)
File "/Users/valentinjungbluth/Desktop/Django/Etat_civil/BirthCertificate/views.py" in BirthCertificate_Form
55. return render(request, 'BC_form.html', {'Bform' : Bform})
File "/usr/local/lib/python2.7/site-packages/django/shortcuts.py" in render
30. content = loader.render_to_string(template_name, context, request, using=using)
File "/usr/local/lib/python2.7/site-packages/django/template/loader.py" in render_to_string
68. return template.render(context, request)
File "/usr/local/lib/python2.7/site-packages/django/template/backends/django.py" in render
66. return self.template.render(context)
File "/usr/local/lib/python2.7/site-packages/django/template/base.py" in render
206. with context.bind_template(self):
File "/usr/local/Cellar/python/2.7.12_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/contextlib.py" in __enter__
17. return self.gen.next()
File "/usr/local/lib/python2.7/site-packages/debug_toolbar/panels/templates/panel.py" in _request_context_bind_template
51. updates.update(context)
Exception Type: TypeError at /BirthCertificate/formulaire
Exception Value: cannot convert dictionary update sequence element #0 to a sequence
他的剧本就是这样:
def BirthCertificate_Form(request) :
# Fonction permettant de créer le formulaire Acte de Naissance et le remplissage
query_lastname = request.GET.get('lastname')
cache.set('query_lastname', query_lastname, 300)
print cache.get('query_lastname')
if request.method == 'POST':
Bform = BirthCertificateForm(request.POST or None)
if Bform.is_valid() : # Vérification sur la validité des données
if '_save2' in request.POST :
post = Bform.save()
return HttpResponseRedirect(reverse('BC_treated', kwargs={'id': post.id}))
else:
Bform = BirthCertificateForm()
return render(request, 'BC_form.html', {'Bform' : Bform})
答案 0 :(得分:0)
我发现了错误。在我的context_processors.py文件中,我有:
def cached_queries(request):
return {'query_lastname', cache.get('query_lastname')}
而不是
def cached_queries(request):
return {'query_lastname' : cache.get('query_lastname')}
现在看起来很有用,所以我为这个愚蠢的问题道歉...