Django wkhtmltopdf don't reading static files

我是研究僧i 提交于 2020-02-02 11:48:08

问题


I use wkhtmltopdf with django-wkhtmltopdf and I think I have incorrectly serving static files. If I run wkhtmltopdf from the console are properly collected static files and generates a good pdf file:

wkhtmltopdf http://127.0.0.1:8000/dash/test/ test.pdf

"GET /static/base/js/jquery.js HTTP/1.1" 200 93106

"GET /static/base/css/bootstrap.css HTTP/1.1" 200 119892

"GET /static/base/js/bootstrap.min.js HTTP/1.1" 200 27726

"GET /static/dash/css/flot.css HTTP/1.1" 200 1810

"GET /static/dash/js/jquery.flot.categories.js HTTP/1.1" 200 6033

"GET /static/dash/js/jquery.flot.js HTTP/1.1" 200 119052

However, if the generated PDF file from your application using django-wkhtmltopdf I have only:

"GET /dash/test/ HTTP/1.1" 200 13246004

And the generated PDF file looks like this:

Static files in settings.py I have set up as follows:

MEDIA_ROOT = ''

MEDIA_URL = ''

STATIC_ROOT = '/Users/malt/Django/env/app/static/'

STATIC_URL = '/static/'

STATICFILES_DIRS = ( )

STATICFILES_FINDERS = ( )

What else should I check?


回答1:


For static files, and for all settings I use something like this (in settings.py):

# settings.py
import os

BASE_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))


def ABS_DIR(rel):
    return os.path.join(BASE_DIR, rel.replace('/', os.path.sep))

MEDIA_ROOT = ABS_DIR('project_name/site_media/')
MEDIA_URL = '/site_media/'
STATIC_ROOT = ABS_DIR('project_name/static/')
STATIC_URL = '/static/'

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
STATICFILES_DIRS = (
    ABS_DIR('project_static'),
)

and in urls.py (a part of it):

# urls.py
from django.conf import settings
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf.urls.static import static

# for dev static files serving
if settings.DEBUG:
    urlpatterns += staticfiles_urlpatterns()
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

It's tested and working. Maybe you have a problem with paths in PDF class?




回答2:


I had a similar issue, and the fix for me was using the full url as my STATIC_URL.

So, instead of:

STATIC_URL = '/static/'

I had to use:

STATIC_URL = 'http://localhost:8000/static/'


来源:https://stackoverflow.com/questions/19445486/django-wkhtmltopdf-dont-reading-static-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!