404: NOT FOUND Vercel

huangapple go评论53阅读模式
英文:

404: NOT FOUND Vercel

问题

我在Vercel上使用GitHub部署了一个简单的Django应用。构建成功,但网站显示404: 未找到。

我在settings.py中编辑了ALLOWED_HOSTS

ALLOWED_HOSTS = ['vercel.app']

404: NOT FOUND Vercel

英文:

I depolyed a simple django app on vercel with github. The build was successful, but the site says 404: NOT FOUND

I edited ALLOWED_HOSTS in my settings.py

ALLOWED_HOSTS = ['vercel.app']

404: NOT FOUND Vercel

答案1

得分: 0

将Django应用部署到Vercel涉及许多配置。由于Vercel没有Django的构建配置,您需要自己配置。

创建一个vercel.json文件,然后放入以下内容:

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

vercel.json作为您的Django应用的配置文件。它定义了Python版本、应用的最大大小以及静态文件的位置。

创建build.sh文件以执行构建命令:

#!/bin/bash

# 更新pip
echo "更新pip..."
python3.9 -m pip install -U pip

# 安装依赖项
echo "安装项目依赖项..."
python3.9 -m pip install -r requirements.txt

# 进行迁移
echo "执行迁移..."
python3.9 manage.py makemigrations --noinput
python3.9 manage.py migrate --noinput

# 收集静态文件
echo "收集静态文件..."
python3.9 manage.py collectstatic --noinput --clear

echo "构建过程完成!"

build.sh文件会更新Vercel中安装的pip的当前版本,安装应用程序的依赖项,执行数据库迁移,然后收集静态文件。

wsgi.py中添加:

app = application

settings.py中:

ALLOWED_HOSTS = [".vercel.app"]
...
STATIC_ROOT = BASE_DIR / "staticfiles" / "static"

完成这些配置后,可以再次部署您的应用。

英文:

Deploying a Django app to Vercel involves a lot of configuration. Since Vercel doesn't have a build config for Django, you would have to do that yourself.

create a vercel.json file, then put this

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

vercel.json serves as a configuration file for your Django application. It defines the Python version, the app's max size, and where to find static files.

create build.sh for the build commands

#!/bin/bash

# Update pip
echo "Updating pip..."
python3.9 pip install -U pip

# Install dependencies

echo "Installing project dependencies..."
python3.9 -m pip install -r requirements.txt

# Make migrations
echo "Making migrations..."
python3.9 manage.py makemigrations --noinput
python3.9 manage.py migrate --noinput

# Collect staticfiles
echo "Collect static..."
python3.9 manage.py collectstatic --noinput --clear

echo "Build process completed!"

The build.sh updates the current version of pip installed in vercel, installs your application's dependencies, makes migrations to your database, and then collects static files.

In wsgi.py add

app = application 

In settings.py

ALLOWED_HOSTS = [".vercel.app"]
...
STATIC_ROOT = BASE_DIR / "staticfiles" / "static"

Deploy your app again when this configurations have been saved

huangapple
  • 本文由 发表于 2023年4月10日 20:49:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75977283.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定