Reputation: 21
Well, I'm new on this. I want to know if I can display a PDF file in my Django template using embed tag like:
<embed src="/file/to/pdf/some_pdf.pdf" />
I tried:
<embed src="{{form.file_path.value}}" width="500" height="525" />
Where {{form.file_path.value}} is my pdf file directory or name saved in the database.
This is the forms.py file:
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'}),
}
And this is the views.py file:
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
Upvotes: 1
Views: 1007
Reputation: 21
I solved it! I did something like:
<embed src="{%static '/previews/'%}{{form.file_path.value}}#toolbar=0&navpanes=0&scrollbar=0" width="500" height="525" />
Upvotes: 1