Location of static files when creating a Django exe using pyinstaller

天涯浪子 提交于 2019-12-30 10:33:44

问题


I have a Django project with the following structure: root videos static templates

and the STATIC_URL setting in settings.py is STATIC_URL = '/static/'

I managed to use pyinstaller to create a windows executable from manage.py. I can start the Django server but I can't figure out where to put the static files.

When I first started the server it could not find the templates as well, it searched for them in : 'root\django\contrib\admin\templates\videos\' I copied the templates to this folder and it worked. But I can't figure out where to put the static files. I tried putting them in 'root\django\contrib\admin\static' to recreate the original structure. But it seems it doesn't search for them there...

Anyone knows where the static files should go? Or how to define the place where a bundled pyinstaller exe will look for them ?


回答1:


please see https://docs.djangoproject.com/en/1.10/howto/static-files/

in your urls.py file, you should add something like blew

from django.conf import settings

from django.conf.urls.static import static

urlpatterns = [
url(r'^login$', login, name='login'),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

and in your app.spec file, add datas

datas=[('xxx/templates','xxx/templates'),
       ('xxx/static','xxx/static')],



回答2:


First make sure that django.contrib.staticfiles is included in your INSTALLED_APPS in settings.py. Then have to insert in your settings.py for example this one:

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static"),
]

I hope definition of BASE_DIR you have in the top of settings.py. BASE_DIR points to a folder, where your manage.py file exists. So if you would have static files in the same dir, then you leave above setting as it is. If not, let's say in the same dir, next to manage.py you will have folder named app, then your settings should look like:

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "app/static"),
]

And you can even run:

python manage.py collectstatic

To get admin static files into your static directory.

Hope that helps.




回答3:


This looks like it might be an issue with where the PyInstaller packed python script looks for static files. They are placed in a temporary folder and need to be accessed by an absolute path. Check out this issue: Bundling data files with PyInstaller (--onefile)



来源:https://stackoverflow.com/questions/39193073/location-of-static-files-when-creating-a-django-exe-using-pyinstaller

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