You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Heroku部署Django应用时加载SpatiaLite库扩展失败

解决Heroku部署Django+SpatiaLite的库加载错误问题

1. 给Heroku环境安装SpatiaLite依赖

Heroku默认没预装SpatiaLite库,得通过以下步骤补上:

  • 添加Heroku Apt Buildpack:
    heroku buildpacks:add --index 1 heroku-community/apt
    
  • 在项目根目录新建Aptfile文件,写入:
    libsqlite3-mod-spatialite
    

2. 修正settings.py配置

你的现有配置有两个问题:django_heroku.settings()的调用顺序可能覆盖数据库设置,且没指定SpatiaLite库的准确路径。调整后的配置如下:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}

# 先判断数据库引擎再应用Heroku配置,避免设置被覆盖
if DATABASES['default']['ENGINE'] in ('django.db.backends.postgresql', 'django.db.backends.postgresql_psycopg2'):
    DATABASES['default']['ENGINE'] = 'django.contrib.gis.db.backends.postgis'
elif DATABASES['default']['ENGINE'] == 'django.db.backends.sqlite3':
    DATABASES['default']['ENGINE'] = 'django.contrib.gis.db.backends.spatialite'
    # 指定Heroku上SpatiaLite库的绝对路径
    DATABASES['default']['OPTIONS'] = {
        'libname': '/usr/lib/x86_64-linux-gnu/mod_spatialite.so',
    }

# 启用GeoDjango参数
django_heroku.settings(locals(), geodjango=True)

3. 重新部署验证

  • 把修改后的settings.py和Aptfile提交到Git
  • 重新部署到Heroku:
    git add .
    git commit -m "fix spatiaLite library loading"
    git push heroku main
    
  • 运行迁移命令确认问题解决:
    heroku run python manage.py migrate
    

内容的提问来源于stack exchange,提问作者Bartosz Goodman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 07:37:38