使用带有Django的embed标签进行PDF预览

时间:2017-05-08 22:28:51

标签: django pdf

嗯,我对此很新。 我想知道我是否可以使用embed标签在我的Django模板中显示PDF文件,如:

<embed src="/file/to/pdf/some_pdf.pdf" />

我试过了:

<embed src="{{form.file_path.value}}" width="500" height="525" />

{{form.file_path.value}}是我的pdf文件目录或保存在数据库中的名称。

这是 forms.py 文件:

fields = [
            'title',
            'publication_date',
            'file_path',
            'size',
            'authors',
            'editorial',
        ]
        labels = {
            'title': 'Title',
            'publication_date': 'Publication Date',
            'file_path': 'File preview',
            'size': 'Size',
            'authors': 'Authors',
            'editorial': 'Editorial',
        }

        widgets = {
            'title':forms.TextInput(attrs={'class':'form-control'}),
            'publication_date':forms.TextInput(attrs={'class':'form-control'}),
            'file_path':forms.TextInput(attrs={'class':'form-control'}),
            'size':forms.TextInput(attrs={'class':'form-control'}),
            'authors': forms.CheckboxSelectMultiple(),
            'editorial': forms.Select(attrs={'class':'form-control'}),
        }

这是 views.py 文件:

class BookList(ListView):
    model = Book
    template_name = 'book/book_list.html'
    context_object_name = 'book'
    queryset = Book.objects.prefetch_related('authors')
    paginate_by = 10
    def get_context_data(self, **kwargs):
        context = super(BookList,self).get_context_data(**kwargs)
        book_list =Book.objects.all()
        paginator = Paginator(book_list,self.paginate_by)

        page = self.request.GET.get('page')
        try:
            book_list = paginator.page(page)
        except PageNotAnInteger:
            file_book = paginator.page(1)
        except EmptyPage:
            book_list = paginator.page(paginator.num_pages)

        context['book_list'] = book_list
        return context

1 个答案:

答案 0 :(得分:0)

我解决了! 我做了类似的事情:

<embed src="{%static '/previews/'%}{{form.file_path.value}}#toolbar=0&navpanes=0&scrollbar=0" width="500" height="525" />