2013-11-27 35 views
0

我views.py鱈魚沒有屬性:屬性Error對象有cleaned_data

def update_details(request): 
    if request.method == "POST": 
      form = UpdateDetailsForm(request.POST) 
      if form.is_valid: 
       asset_code=form.cleaned_data['asset_code1'] 
       fd=form.cleaned_data['product_details'] 
       verifications = Verification.objects.filter(asset_code__exact=asset_code) 
       verifications.update(product_details=fd) 

    return render_to_response('update_details.html', 
       {'form':UpdateDetailsForm(),}, 
       context_instance=RequestContext(request)) 

我想在我的模型更新「product_details」列值,其中資產代碼正好是輸入的內容的用戶。但是當我提交按鈕時出現錯誤。

錯誤消息:

AttributeError的對象沒有屬性 'cleaned_data' django的

+0

重複http://stackoverflow.com/questions/4308527/django-model-form-object-has-no-attribute-cleaned-data – Chandan

+0

@Chandan的 - 不是重複。例外情況相同,但原因不同。 –

+0

我的道歉。看到答案後,我意識到自己的錯誤。 – Chandan

回答

3

form.is_valid是一種方法;你需要調用它:

from django.shortcuts import render, redirect 

def update_details(request): 
    if request.method == "POST": 
      form = UpdateDetailsForm(request.POST, request.FILES) 
      if form.is_valid(): 
       asset_code=form.cleaned_data['asset_code1'] 
       fd=form.cleaned_data['product_details'] 
       verifications = Verification.objects.filter(asset_code__exact=asset_code) 
       # filter returns a list, so the line below will not work 
       # you need to loop through the result in case there 
       # are multiple verification objects returned 
       # verifications.update(product_details=fd) 
       for v in verifications: 
        v.update(product_details=fd) 

       # you need to return something here 
       return redirect('/') 
      else: 
       # Handle the condition where the form isn't valid 
       return render(request, 'update_details.html', {'form': form}) 

    return render(request, 'update_details.html', {'form':UpdateDetailsForm()}) 
+0

錯誤不是這樣,雖然 – aIKid

+1

不,這是正確的答案。在調用is_valid'之前,'cleaned_data'不存在,並且在不調用它的情況下將其檢查爲布爾值將返回'True',但不會創建'cleared_data'。 –