Django wkhtmltopdf不读取静态文件 [英] Django wkhtmltopdf don't reading static files

查看:159
本文介绍了Django wkhtmltopdf不读取静态文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将wkhtmltopdf与django-wkhtmltopdf一起使用,我认为我错误地提供了静态文件。
如果我从控制台运行wkhtmltopdf,则正确收集了静态文件并生成了良好的pdf文件:

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/js/jquery.js HTTP/1.1" 200 93106

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

"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/base/js/bootstrap.min.js HTTP/1.1" 200 27726

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

"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.categories.js HTTP/1.1" 200 6033

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

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

但是,如果使用django-wkhtmltopdf从您的应用程序生成的PDF文件只有我:

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

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

生成的PDF文件如下:

And the generated PDF file looks like this:

settings.py中的静态文件我的设置如下:

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

MEDIA_ROOT =' '

MEDIA_ROOT = ''

MEDIA_URL =''

MEDIA_URL = ''

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

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

STATIC_URL ='/ static /'

STATIC_URL = '/static/'

STATICFILES_DIRS =(

STATICFILES_DIRS = ( )

STATICFILES_FINDERS =(

STATICFILES_FINDERS = ( )

我还应该检查什么?

推荐答案

对于静态文件和所有设置,我都使用这样的设置(在settings.py中):

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'),
)

并在urls.py中(部分):

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)

已经过测试并且可以正常工作。也许您对PDF类中的路径有疑问?

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

这篇关于Django wkhtmltopdf不读取静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆