安装:
pip install pyinstaller
在urls.py
from SheepMasterOneToOne import settings
from django.conf.urls.static import static
urlpatterns = [
path("admin/", admin.site.urls),
path('generate_report/export/', ReportAdmin(models.Report, admin.site).generate_report, name='generate_report'),
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
在settings.py
STATIC_URL = "static/"
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
收集静态文件
python manage.py collectstatic
在项目根目录下
pyinstaller manage.py
生成
如果报错就修改manage.spec
# -*- mode: python ; coding: utf-8 -*-
a = Analysis(
['manage.py'],
pathex=['/path/to/your/'],
binaries=[],
datas=[
('/path/to/your//venv/lib/python3.9/site-packages/simpleui/static', 'simpleui/static'),
('/path/to/your/staticfiles', 'staticfiles')
],
hiddenimports=['sheep.apps'],
hookspath=[],
hooksconfig={},
runtime_hooks=[],
excludes=[],
noarchive=False,
optimize=0,
)
pyz = PYZ(a.pure)
exe = EXE(
pyz,
a.scripts,
a.binaries,
a.datas,
[],
name='manage',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
upx_exclude=[],
runtime_tmpdir=None,
console=True,
disable_windowed_traceback=False,
argv_emulation=False,
target_arch=None,
codesign_identity=None,
entitlements_file=None,
)
最后运行
./dist/manage runserver 8080 --noreload
启动成功