Python 提交表单时出错

Python 提交表单时出错,python,python-2.7,django-forms,django-1.4,django-sessions,Python,Python 2.7,Django Forms,Django 1.4,Django Sessions,可能重复: 我正在编辑表单并提交它,但它没有得到更新,而是存储在另一个条目中 def searchinventory2(request, id = None): print "sssssssssss", id if id is not None: task = Inventory.objects.get(id = id) else: pass if request.method == 'POST': # If t

可能重复:

我正在编辑表单并提交它,但它没有得到更新,而是存储在另一个条目中

def searchinventory2(request, id = None):
    print "sssssssssss", id

    if id is not None:
        task = Inventory.objects.get(id = id)
    else:
           pass     
    if request.method == 'POST': # If the form has been submitted...
        print "dfdf"
        form = Inventory_List(request.POST, request.FILES, instance=task)
        print "hhhhhhhhhhhhh"
        if form.is_valid():
            print "ksjhjksfh"    
            form.save()
            return task(request, 'Task #%s created successfully.' % (task.id))
    else:
        print "in else"
        form = Inventory_List(instance = task)
    return render_to_response("smartlogis/inventoryedit.html", {'form':form,})
我得到这个错误:


仅当
时,才会初始化分配前引用的
局部变量“task”

任务
,因此,如果调用
搜索清单2(请求)
,则任务不会初始化

你应该这样做:

if id is not None:
    task = Inventory.objects.get(id = id)
else:
    task = something that can be then used 

第一:调整缩进(你必须在所有代码部分添加4个空格)。第二:我看不出标题和问题之间的联系