django 属性错误对象没有属性cleaned_data

9jyewag0  于 2023-03-04  发布在  Go
关注(0)|答案(2)|浏览(183)

我的www.example.com代码: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))

我想更新模型中的"product_details"列值,其中资产代码与用户输入的代码完全相同。但当我按提交按钮时,出现错误。
错误信息:
AttributeError对象没有属性'cleaned_data' django

iovurdzv

iovurdzv1#

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()})
z9smfwbn

z9smfwbn2#

似乎您忘记在后面添加(),如果form.is_valid:
将您的代码替换为:

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

我希望这能帮上忙

相关问题