0

爲題說,我在一個管理視圖
這裏使用模板有一個問題是我worktree
的Django 1.11模板使用

project 
|-- project/ 
|-- myapp/ 
    |-- templates/ 
     |-- admin/ 
      |-- file.html 

settings.py

TEMPLATES = [ 
    { 
     'BACKEND': 'django.template.backends.django.DjangoTemplates', 
     'DIRS': [os.path.join(BASE_DIR, 'myapp/templates')], 
     'APP_DIRS': True, 
     'OPTIONS': { 
      'context_processors': [ 
       'django.template.context_processors.debug', 
       'django.template.context_processors.request', 
       'django.contrib.auth.context_processors.auth', 
       'django.contrib.messages.context_processors.messages', 
      ], 
     }, 
    }, 
] 

ModelAdmin.py

class ModelAdmin(admin.ModelAdmin): 
    actions = ['export'] 

    def export(self,request, queryset): 
     paragraphs = ['first paragraph', 'second paragraph', 'third paragraph'] 
     pdfkit.from_file('file.html', 'out.pdf', paragraphs) 
admin.site.register(Model, ModelAdmin) 

但我我得到 「沒有這樣的文件:file.html」 錯誤 enter image description here

enter image description here

+0

您可以發佈錯誤日誌。 –

+0

請不要將錯誤消息作爲圖像發佈。複製並粘貼文本。 – Alasdair

+0

你不需要在'DIRS'中包含'myapp/templates'。只要'myapp'在'INSTALLED_APPS'中,它就會被app目錄加載器檢查。 – Alasdair

回答

0

嘗試:

pdfkit.from_file('admin/file.html', 'out.pdf', paragraphs) 

我認爲該文件是內部templates/admin,而不是內部templates

+0

這是相同的錯誤 – leila

0

pdfkit不知道您的TEMPLATES設置。您需要提供與您的項目目錄相關的路徑。

pdfkit.from_file('myapp/templates/admin/file.html', 'out.pdf', paragraphs) 

但是,這會將file.html視爲html文件。如果它是要呈現的Django模板,則可以使用render_to_stringfrom_string

from django.template.loader import render_to_string 
html = render_to_string(request, 'admin/file.html', {...}) 
pdfkit.from_string(html, 'out.pdf') 
+0

不工作得到「TemplateDoesNotExist」錯誤 – leila

+0

這不是足夠的信息。完整的錯誤信息應該告訴你Django試過哪些目錄,這應該有助於調試問題。使用諸如'myapp'和'file.html'這樣的組合名稱使得它更難以提供幫助。 – Alasdair

+0

我結束了在modelAdmin中使用html – leila