django-wkhtmltopdf
django-wkhtmltopdf copied to clipboard
Static Files only found if collectstatic has been run
PDF's can't be created using static assets and django-wkhtmltopdf unless collectstatic has been run, moving the static files into the STATIC_ROOT directory.
This isn't as per django template views in debug mode where static files are served out of their initial directory and makes development more difficult where pdfs depend on css for instance.
This issue is deeply tied to the way that django treats static files in debug mode with runserver
; django-wkhtmltopdf maintainers may be ok with not supporting this usage.
A workaround I have found is this: create a symlink from wherever STATIC_ROOT
is to the "initial" directory of static files. For example, if you settings says:
import os
STATICFILES_DIR = ['path/to/some/dir']
STATIC_ROOT = os.path.abspath('static')
Notes:
- The
STATIC_ROOT
variable is ignored byrunserver
in debug mode, - we cannot just make
STATIC_ROOT
be/path/to/some/dir
since that crashes django system checks, which makes sense because you can't haveSTATIC_ROOT
(output ofcollectstatic
) to be insideSTATICFILES_DIR
(input ofcollectstatic
). A symbolic link, however, tricks the system check into not realizing that the two paths are the same. - Even if you do run
collectstatic
in this setup you end up copying every single static files on top of itself; nothing will break. - This will break if you have multiple
STATICFILES_DIR
paths; because you can only symlink to one.
A somewhat better option is to use the collectstatic -l
built-in flag so that collectstatic creates the symlinks to each file individually, as opposed to the developer symlinking the whole static folder. The downside is that you'll have to re-run collectstatic if you create new static files.