Django wkhtmltopdf don't reading static files

限于喜欢 提交于 2019-12-06 07:42:38

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?

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