Pyinstaller not able to locate static files

孤者浪人 提交于 2021-02-08 11:38:53

问题


I have created djnago project followong is directory structure of project

mysite
--settings.py
-- urls.py
--wsgi.py

polls(app)
--static
  --polls
    --images
--templates
  --polls
    index.html
    results.html
admin.py
apps.py
models.py
urls.py
views.py

I have created installer using pyinstaller that exe runs fine able to load templates But for static files(css and js),exe giving me error

C:\Users\sanjad\Desktop\testdemo\myinstaller>dist\manage\manage.exe runserver
Performing system checks...

System check identified no issues (0 silenced).
January 03, 2017 - 10:10:15
Django version 1.10.2, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
[03/Jan/2017 10:10:18] "GET / HTTP/1.1" 200 346
Not Found: /static/polls/style.css
[03/Jan/2017 10:10:18] "GET /static/polls/style.css HTTP/1.1" 404 2605

Following is my .spec file

# -*- mode: python -*-

block_cipher = None


a = Analysis(['..\\mysite\\manage.py'],
             pathex=['C:\\Users\\sanjad\\Desktop\\testdemo\\myinstaller'],
             binaries=None,
            datas=[
                    ('C:\\Users\\sanjad\\Desktop\\testdemo\\mysite\\polls\\templates\\','polls\\templates'),
                    ('C:\\Users\\sanjad\\Desktop\\testdemo\\mysite\\polls\\static\\','polls\\static')
                    ],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          exclude_binaries=True,
          name='manage',
          debug=False,
          strip=False,
          upx=True,
          console=True )
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas,
               strip=False,
               upx=True,
               name='manage')

回答1:


Have you seen the answer given by fpx006 here? https://github.com/pyinstaller/pyinstaller/issues/2368 I made a minor change to it as follows (also posted in that thread).

Add this to my top level urls.py

urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT).

I had done the same thing to get my media files served with

urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)'

And then add both of those to my spec file, along with my migrations folder as follows:

datas=[('filter/filter/core/migrations','filter/core/migrations'),
('filter/media_root','media_root'),('filter/static','static_root')],

where core is the name of the only app in this Django project, and filter is the name of the project. It is a bit of a hack, but should allow you to distribute your Django app in a .exe and see your beautiful styling.



来源:https://stackoverflow.com/questions/41436658/pyinstaller-not-able-to-locate-static-files

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