'Pyinstaller doesn't include django rest framework templates
I have a django app that uses rest_framework, and everything works perfectly, I am using pyinstaller to get an exe for this app, the executable app works fine, but when I try to visit a Browsable api for the rest framework such http://localhost:8000/api/flags/ to get a view of the Browsable api, I get this error
TemplateDoesNotExist at /api/flags/
rest_framework/horizontal/form.html
Request Method: GET
Request URL: http://localhost:8000/api/flags/
Django Version: 3.0.7
Exception Type: TemplateDoesNotExist
Exception Value:
rest_framework/horizontal/form.html
Exception Location: site-packages\django\template\loader.py in get_template, line 19
Python Executable: C:\Workset\proj_test\app.exe
Python Version: 3.8.3
Python Path:
['C:\\Workset\\proj_test\\dist\\app\\base_library.zip',
'C:\\Workset\\proj_test\\dist\\app']
Server time: Thu, 9 Jul 2020 09:52:53 +0000
Template-loader postmortem
Django tried loading these templates, in this order:
Using engine django:
django.template.loaders.filesystem.Loader: C:\Workset\proj_test\dist\app\src\templates\rest_framework\horizontal\form.html (Source does not exist)
django.template.loaders.app_directories.Loader: C:\Workset\proj_test\dist\app\django\contrib\admin\templates\rest_framework\horizontal\form.html (Source does not exist)
django.template.loaders.app_directories.Loader: C:\Workset\proj_test\dist\app\django\contrib\auth\templates\rest_framework\horizontal\form.html (Source does not exist)
This means that Pyinstaller doesn't include the rest_framework default ui templates, how I can include this templates in the output dist folder generated by Pyinstaller
Note: the rest api itself works fine, i.e calling the rest api works fine
Solution 1:[1]
I know this is sort of an older post, but thought I would share my resolution.
I had found the django specific hook file located here (linux machine): /home/<< user >>/.local/lib/python3.6/site-packages/PyInstaller/hooks/hook-django.py
Discovered that it only pulled anything in the django site-packages folder. Django rest_framework is not included in the django site-packages, it is in it's own folder on the same level. Thus, I edited this hook file to include the rest_framework folder by adding the following code:
datas, binaries, hiddenimports = collect_all('django')
datas_rest, binaries_rest, hiddenimports_rest = collect_all('rest_framework')
datas += datas_rest
binaries += binaries_rest
hiddenimports += hiddenimports_rest
Then re-run the pyinstaller. Ensure it included the rest_framework folder in the dist folder.
Solution 2:[2]
in your manage.spec file, you need add such code below:
from PyInstaller.utils.hooks import collect_submodules
hiddenimports = collect_submodules('rest_framework')
and also you need add another Dependency packages
hiddenimports.extend(
[
'django.contrib.contenttypes',
'django.contrib.staticfiles',
'django_filters',
....
]
)
and if you rest-freamwork template shows doesn't exist, so you need config the static files as blew?
datas=[(r'/env/lib/python3.6/site-packages/rest_framework/', './rest_framework'),
(r'/env/lib/python3.6/site-packages/django_filters/', './django_filters')
]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Cherylee Sanchez |
Solution 2 | Enzoooo_ |