gpt4 book ai didi

python - UnboundLocalError - 赋值前引用的局部变量 - Django

转载 作者:行者123 更新时间:2023-11-28 20:52:16 26 4
gpt4 key购买 nike

<分区>

你们这周帮了我很多,真是太棒了。希望你们能帮我解决这个问题。我试着查看网站上有关此类错误的其他帖子,但没有一篇真正帮助我解决问题。基本上我在一个页面上提交一个表单,POST 数据将进入与生成它相同的 View 。我想在发送 POST 数据后重定向到另一个页面,但出现此错误。

def test(request):
if request.method == 'POST':
form = SubmitForm(request.POST) # A form bound to the POST data
if form.is_valid():
lat = form.cleaned_data['lat']
lng = form.cleaned_data['lng']
title = form.cleaned_data['title']
story = form.cleaned_data['story']

ctxt = {
'lat':lat,
'lng':lng,
'title':title,
'story':story,
}

return render_to_response('home.html', ctxt, context_instance=RequestContext(request))

else:

import datetime
now = datetime.datetime.now()
form = SubmitForm()
latest_marks = Marker.objects.all().order_by('-submitted')[0:10]

ctxt = {
'marks':latest_marks,
'now':now.date(),
'form': form,
}
return render_to_response('test.html', ctxt, context_instance=RequestContext(request))

我得到的错误是

local variable 'ctxt' referenced before assignment

回溯是

Environment:


Request Method: POST
Request URL: http://localhost:8000/test/

Django Version: 1.3
Python Version: 2.6.5
Installed Applications:
['django.contrib.staticfiles',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.admin',
'userena',
'guardian',
'easy_thumbnails',
'south',
'database',
'accounts',
'socialregistration']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'userena.middleware.UserenaLocaleMiddleware',
'socialregistration.middleware.FacebookMiddleware')


Traceback:
File "/home/wluw/dev/chicagomap/lib/python2.6/site-packages/Django-1.3-py2.6.egg/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/home/wluw/dev/chicagomap/chicagomap/../chicagomap/database/views.py" in test
58. return render_to_response('test.html', ctxt, context_instance=RequestContext(request))

Exception Type: UnboundLocalError at /test/
Exception Value: local variable 'ctxt' referenced before assignment

如有任何帮助,我们将不胜感激。谢谢。

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