Pyinstaller无法找到静态文件

时间:2017-01-03 04:56:31

标签: pyinstaller

我创建了djnago项目followong是项目的目录结构

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

我使用pyinstaller创建了安装程序 exe运行良好,能够加载模板 但对于静态文件(css和js),exe给我错误

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

以下是我的.spec文件

# -*- 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 个答案:

答案 0 :(得分:1)

你有没有看到fpx006给出的答案? https://github.com/pyinstaller/pyinstaller/issues/2368我对它进行了如下的小改动(也发布在该帖子中)。

将此添加到我的顶级urls.py

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

我做了同样的事情来提供我的媒体文件

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

然后将这两个文件与我的迁移文件夹一起添加到我的spec文件中,如下所示:

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

其中core是此Django项目中唯一应用程序的名称,filter是项目的名称。这有点像黑客,但应该允许你在.exe中发布你的Django应用程序,看看你漂亮的造型。