Django列表索引超出范围

时间:2019-05-10 09:57:31

标签: python django

我正在开发一个django项目,该项目允许用户上传图像和视频,图像和视频都可以上传,但是当用户尝试在帖子中编辑图像时,它将返回错误。例如,当用户尝试删除“一个”图像时,它返回“列表索引超出范围”

  

索引错误列表索引超出范围

#views.py 

class EditView(LoginRequiredMixin, UserPassesTestMixin, UpdateView):
    model = Post
    fields = ['title', 'content', 'restrict_comment']
    template_name = 'blog/postupdate.html'

    def get(self, request, *args, **kwargs):
        pk = kwargs['pk']
        post = get_object_or_404(Post, pk=pk)
        form = PostEditForm(instance=post)
        ImageFormset = modelformset_factory(Images, fields=('image',), extra=4, max_num= 4)
        formset = ImageFormset(queryset=Images.objects.filter(post=post))

        context = {
            'form': form,
            'post': post,
            'formset': formset,
        }
        return render(request, 'blog/postupdate.html', context)

    def post(self, request, *args, **kwargs):
        pk = kwargs['pk']
        post = get_object_or_404(Post, pk=pk)
        ImageFormset = modelformset_factory(Images, fields=('image',), extra=4, max_num=4)
        if request.method == 'POST':
            form = PostEditForm(request.POST or None, request.FILES or None, instance=post)
            formset = ImageFormset(request.POST or None, request.FILES or None)
            # print(form.is_valid())
            print(formset.is_valid())
            if form.is_valid() and formset.is_valid():
                form.save()
                # print(formset.cleaned_data)
                data = Images.objects.filter(post=post)
                for index, f in enumerate(formset):
                    if f.cleaned_data:
                        if f.cleaned_data['id'] is None:
                            photo = Images(post=post, image=f.cleaned_data.get('image'))
                            photo.save()

                        elif f.cleaned_data['image'] is False:
                            photo = Images.objects.get(id=request.POST.get('form-' + str(index) + "-id"))
                            photo.delete()
                        else:
                            photo = Images(post=post, image=f.cleaned_data.get('image'))
                            d = Images.objects.get(id=data[index].id)
                            d.image = photo.image
                            d.save()
                return HttpResponseRedirect(reverse('post-detail', kwargs={'pk': pk}))  # HttpResponseRedirect(post.get_absolute_url())
        else:
            form = PostEditForm(instance=post)
            formset = ImageFormset(queryset=Images.objects.filter(post=post))

        context = {
            'form': form,
            'post': post,
            'formset': formset,
        }
        return HttpResponseRedirect(post.get_absolute_url())

    def test_func(self):
        post = self.get_object()
        if self.request.user == post.user:
            return True
        return False

    def form_valid(self, form):
        form.instance.user = self.request.user
        return super().form_valid(form)


我希望编辑过程正常进行,而不会出现任何错误。非常感谢您的协助。 谢谢

0 个答案:

没有答案