一、前期环境配置
1、准备Django项目
(1)创建django项目
(2)创建 requirements.txt:
pip freeze > requirements.txt
(3)创建 runtime.txt:echo "python-3.9.6" > runtime.txt
(4)创建 vercel.json{ "builds": [ { "src": "demo01/wsgi.py", "use": "@vercel/python" } ], "routes": [ { "src": "/(.*)", "dest": "demo01/wsgi.py" } ] }
(5)修改设置
ALLOWED_HOSTS = ['*']
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
(6)wsgi.py设置
import os from django.core.wsgi import get_wsgi_application os.environ.setdefault("DJANGO_SETTINGS_MODULE", "demo01.settings") application = get_wsgi_application() app = application
2、设置Vercel
(1)安装Vercel
npm i -g vercel
(2)登录Vercel
vercel login
(3)部署以及后续更新
生产环境运行:
vercel --prod
测试环境运行:
vercel
开发环境运行:
vercel dev
二、遇到的问题
(1)部署时会有选择
(2)项目结构
my_django_project/
├── my_app/
│ ├── init.py
│ ├──
admin.py
│ ├──
apps.py
│ ├──
models.py
│ ├──
tests.py
│ ├──
views.py
│ └── migrations/
├── my_django_project/
│ ├── init.py
│ ├──
settings.py
│ ├──
urls.py
│ ├──
wsgi.py
│ └──
asgi.py
├── static/
├── media/
├──
manage.py
├── requirements.txt
├── runtime.txt
└── vercel.json