django 使用Vercel时无法加载静态文件

pn9klfpd  于 2023-07-01  发布在  Go
关注(0)|答案(1)|浏览(133)

当我在本地托管我的项目时,一切都正常。我的样式表加载没有问题,但是当我将它部署到Vercel时,管理静态文件和我自己的加载都没有问题。这里是我的settings.py:

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles_build', 'static')
MEDIA_URLS ='/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

我的vercel.json:

{
    "builds": [{
        "src": "social/wsgi.py",
        "use": "@vercel/python",
        "config": { "maxLambdaSize": "15mb", "runtime": "python3.9" }
    },
    {
        "src": "build_files.sh",
        "use": "@vercel/static-build",
        "config": { "distDir": "staticfiles_build" }
    }],
    "routes": [
        {
            "src": "/static/(.*)",
            "dest": "/static/$1"
        },
        {
            "src": "/(.*)",
            "dest": "social/wsgi.py"
        }
    ]
}

到目前为止,我已经运行了collect static命令,并尝试更改根目录和URL,但我非常困惑,不完全理解为什么它不起作用。

iugsix8n

iugsix8n1#

我也遇到过类似的问题。您可能需要为静态文件指定输出目录:

{
    "builds": [{
        "src": "social/wsgi.py",
        "use": "@vercel/python",
        "config": { "maxLambdaSize": "15mb", "runtime": "python3.9" }
    },
    {
        "src": "build_files.sh",
        "use": "@vercel/static-build",
        "config": { "distDir": "staticfiles_build" }
    }],
    "routes": [
        {
            "src": "/static/(.*)",
            "dest": "/static/$1"
        },
        {
            "src": "/(.*)",
            "dest": "social/wsgi.py"
        }
    ],

    "outputDirectory": "staticfiles_build"
}

相关问题