Attribute Error Object has no attribute cleaned_data

Question:

my views.py cod:

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))

I want to update ‘product_details’ column value in my model where asset code is exactly what user entered. But I am getting error when I submit button.

Error Message:

AttributeError object has no attribute ‘cleaned_data’ django

Asked By: PK10

||

Answers:

form.is_valid is a method; you need to call it:

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()})
Answered By: Burhan Khalid

Seems like you forgot to add () after, if form.is_valid:

replace your code with:

  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))

#i hope this helps

Answered By: Jay Wandery
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.