1
我想編輯一個已經創建的對象(「擔心」)。Django:「對象不可迭代」,當試圖修改所述對象
我有這樣的代碼在views.py:
def worry_edit(request, id):
worry = get_object_or_404(Worry, pk = id)
original_pub_date = worry.pub_date
form = WorryForm(request.POST or None, instance = worry)
if form.is_valid():
worry = form.save(commit = False)
worry.pub_date = original_pub_date
worry.save()
return redirect(worry)
return render_to_response('holaProy/worry_edit.html', {'worry_form': form, 'worry_id': id}, context_instance=RequestContext(request))
擊中形式發送鍵時提供了以下錯誤:
argument of type 'Worry' is not iterable
上爲什麼錯誤出現和任何見解如何解決它?
編輯:由於Girasquid建議,這是完整的回溯:
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/holaProy/worry_edit/1/
Django Version: 1.4.1
Python Version: 2.7.3
Installed Applications:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'django.contrib.admindocs',
'holaProy',
'registration',
'django.contrib.humanize')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')
Traceback:
File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Richard\proyectosPython\holaProyecto\holaProyecto\holaProy\views.py" in worry_edit
50. return redirect(worry)
File "C:\Python27\lib\site-packages\django\shortcuts\__init__.py" in redirect
81. if '/' not in to and '.' not in to:
Exception Type: TypeError at /holaProy/worry_edit/1/
Exception Value: argument of type 'Worry' is not iterable
你可以粘貼完整的回溯? – girasquid
憂慮類是否有get_absolute_url()方法? – girasquid
@girasquid你是對的,我需要添加get_absolue_url()方法。謝謝。 – Xar