2017-05-08 60 views
0

嗯,我是新手。 我想知道如果我能在我的Django的模板使用嵌入標籤像顯示PDF文件:使用嵌入式標籤與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 

回答

0

我解決它! 我做了類似的事情:

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