Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django:“;对象不可编辑";,当试图修改所述对象时_Python_Django_Iterable - Fatal编程技术网

Python Django:“;对象不可编辑";,当试图修改所述对象时

Python Django:“;对象不可编辑";,当试图修改所述对象时,python,django,iterable,Python,Django,Iterable,我正在尝试编辑一个已经创建的对象(“担心”) 我在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(): wo

我正在尝试编辑一个已经创建的对象(“担心”)

我在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

为了能够将模型实例传递到
重定向
,您需要在其上定义一个方法。否则,Django只是假设它是一个字符串形式的URL,并尝试对其进行解析。

要能够将模型实例传递到
重定向
,您需要在其上定义一个方法。否则,Django只是假设它是一个字符串形式的URL并尝试对其进行解析。

您可以粘贴完整的回溯吗?担心类是否有get\u absolute\u URL()方法?@girasquid您是对的,我需要添加get\u absolute\u URL()方法。谢谢。你能粘贴完整的回溯吗?担心类有get\u absolute\u url()方法吗?@girasquid你是对的,我需要添加get\u absolute\u url()方法。谢谢你,谢谢你,丹尼尔。我不知道这个功能。现在它工作得很好。谢谢你,丹尼尔。我不知道这个功能。现在它运行良好。