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

如何在Railway平台部署GitHub仓库子文件夹中的Django后端?

Troubleshooting Django Deployment from a Subfolder in a Monorepo

Let’s walk through the common issues that might be breaking your deployment, and fix them one by one—having both Django and Next.js in a single repo is totally feasible, so let’s get this sorted:

1. Fix the Procfile’s Working Directory & Path

Your current Procfile points to backend.myblog.wsgi, but most deployment platforms (like Heroku, Render, etc.) start running commands from the repo root by default. This means gunicorn can’t locate your Django project properly.

Update your Procfile to first navigate to the backend folder before starting the server:

web: cd backend && gunicorn myblog.wsgi --log-file -

Alternatively, use gunicorn’s --chdir flag to set the working directory directly:

web: gunicorn --chdir backend myblog.wsgi --log-file -

This ensures gunicorn looks for your Django project files inside the backend subfolder.

2. Confirm Dependencies in requirements.txt

Make sure gunicorn is included in your root-level requirements.txt—it’s the WSGI server required to run Django in production. If it’s missing, add it:

gunicorn>=20.1.0
# Add all your other Django dependencies here (Django, psycopg2-binary, etc.)

Double-check that all other required packages are listed correctly too—missing dependencies are a common deployment blocker.

3. Set the DJANGO_SETTINGS_MODULE Environment Variable

Your deployment platform needs to know where to find Django’s settings file. If your settings are in backend/myblog/settings.py, set this environment variable:

DJANGO_SETTINGS_MODULE=myblog.settings

How you set this varies by platform: for Heroku, run heroku config:set DJANGO_SETTINGS_MODULE=myblog.settings in your terminal; for Render, add it in the environment variables section of your deployment dashboard.

4. Update Django’s ALLOWED_HOSTS

In backend/myblog/settings.py, make sure you’ve added your deployment platform’s domain (or localhost for testing—avoid using * in production):

ALLOWED_HOSTS = ['your-deployed-app-domain.com', 'localhost', '127.0.0.1']

For platforms like Heroku, you can make this dynamic with:

import os
ALLOWED_HOSTS = [os.environ.get('ALLOWED_HOSTS', 'localhost')]

Then set the ALLOWED_HOSTS environment variable to your app’s actual domain.

5. Add Deployment-Time Django Commands

Don’t forget to run critical setup steps like database migrations during deployment. Most platforms let you define a "release command"—for example, on Heroku, add this to your Procfile or dashboard:

release: cd backend && python manage.py migrate

This ensures your database schema is up to date every time you deploy.

6. Double-Check Your Folder Structure

Verify your repo matches the structure we’re assuming (adjust paths below if your setup is different):

repo-root/
├── backend/
│   ├── myblog/
│   │   ├── wsgi.py
│   │   ├── settings.py
│   │   └── ...
│   └── manage.py
├── frontend/
│   └── (Next.js project files)
├── requirements.txt
├── runtime.txt
└── Procfile

If your Django project is nested deeper (e.g., backend/my-django-app/myblog/), tweak the paths in your Procfile and environment variables to match.

Once you’ve made these adjustments, redeploy your app—this should resolve most common issues with running Django from a subfolder in a monorepo.

内容的提问来源于stack exchange,提问作者Maryll Castelino

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.27 14:03:14