django-wkhtmltopdf icon indicating copy to clipboard operation
django-wkhtmltopdf copied to clipboard

Static Files only found if collectstatic has been run

Open iwootten opened this issue 5 years ago • 2 comments

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.

iwootten avatar Oct 08 '18 14:10 iwootten

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 by runserver 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 have STATIC_ROOT (output of collectstatic) to be inside STATICFILES_DIR (input of collectstatic). 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.

amirkdv avatar Feb 08 '19 23:02 amirkdv

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.

edu2004eu avatar Dec 02 '21 15:12 edu2004eu