0

as the title said i am having a problem using a template in an admin view
here is my worktree

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)

but i am getting "No such file: file.html" error enter image description here

enter image description here

Community
  • 1
  • 1
leila
  • 461
  • 1
  • 7
  • 21

1 Answers1

0

pdfkit does not know about your TEMPLATES setting. You need to provide the path relative to your project directory.

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

However this will treat the file.html as an html file. If it is a Django template that you want to render, you could use render_to_string and from_string.

from django.template.loader import render_to_string
html = render_to_string(request, 'admin/file.html', {...}) 
pdfkit.from_string(html, 'out.pdf') 
Alasdair
  • 298,606
  • 55
  • 578
  • 516
  • That's not enough information. The full error message should tell you which directories Django tried which should help debug the problem. Using made up names like `myapp` and `file.html` makes it harder to help. – Alasdair Sep 07 '17 at 07:18
  • i ended up using html inside the modelAdmin – leila Sep 09 '17 at 01:37