Pyinstaller无法定位静态文件



我已经创建了djnago项目,下面是项目的目录结构

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:UserssanjadDesktoptestdemomyinstaller>distmanagemanage.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')

你看到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)'

然后将这两个文件以及迁移文件夹添加到我的规范文件中,如下所示:

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

其中core是Django项目中唯一的应用程序的名称,filter是项目的名称。这有点像黑客,但应该允许你在.exe中分发你的Django应用程序,并看到你美丽的风格。

最新更新