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

请教:如何在Netlify上部署cookiecutter-django或Django项目?

Deploying Django or Cookiecutter-Django on Netlify: A Practical Guide

Absolutely! Tons of developers have deployed both regular Django projects and cookiecutter-django setups on Netlify—though it takes a few tweaks since Netlify’s sweet spot is static sites. Let me walk you through exactly what you need to do, including the key commands and configs.

Prerequisites First

  • Make sure your project has a requirements.txt (or pyproject.toml for Poetry, Pipfile for Pipenv) listing all dependencies.
  • For cookiecutter-django, most static file configs are already set up out of the box, but it’s worth double-checking before you start.

Step 1: Add Netlify-Friendly Packages

First, install the tools needed to run Django as a serverless function on Netlify. Add these to your requirements.txt:

django
gunicorn
serverless-wsgi
netlify-lambda

For cookiecutter-django, just append these to your base requirements file.

Step 2: Set Up Netlify Functions

Netlify uses functions to run dynamic code, so we’ll wrap Django’s WSGI app in a function:

  • Create a functions folder in your project root.
  • Inside that folder, make a file called django_app.py with this code:
import os
import sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from django.core.wsgi import get_wsgi_application
from serverless_wsgi import handle_request

# Replace with your actual settings module (e.g., "config.settings.production" for cookiecutter-django)
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "your_project_name.settings")
application = get_wsgi_application()

def handler(event, context):
    return handle_request(application, event, context)

Step 3: Configure Netlify with netlify.toml

Create a netlify.toml file in your project root to define the build and redirect rules. Here’s a working template:

[build]
  # This command collects static files, installs lambda tools, and builds the function
  command = "python manage.py collectstatic --noinput && npm install netlify-lambda && netlify-lambda build functions"
  # Netlify serves static files from this directory
  publish = "staticfiles"

# Redirect all requests to our Django function
[[redirects]]
  from = "/*"
  to = "/.netlify/functions/django_app"
  status = 200

# For local development
[dev]
  command = "netlify-lambda serve functions"
  • Don’t forget to set STATIC_ROOT = BASE_DIR / 'staticfiles' in your Django settings.py (cookiecutter-django already does this for production).

Step 4: Set Up Environment Variables in Netlify

Netlify doesn’t read local .env files by default, so you’ll need to add your Django secrets in the dashboard:

  1. Go to your Netlify site’s Site Settings > Environment Variables
  2. Add variables like:
    • SECRET_KEY: Your Django secret key
    • DATABASE_URL: URL for your remote database (Netlify doesn’t host databases—use something like ElephantSQL or AWS RDS)
    • DEBUG: Set to False for production
    • ALLOWED_HOSTS: Add your Netlify site URL (e.g., your-site-name.netlify.app)

Cookiecutter-Django Specific Notes

If you’re using cookiecutter-django, there are a few small adjustments to make:

  • Use config.settings.production as your DJANGO_SETTINGS_MODULE in django_app.py (that’s the default production config).
  • Ensure your ALLOWED_HOSTS in config/settings/production.py includes your Netlify URL.
  • The default collectstatic setup should work, but confirm STATIC_ROOT is set to BASE_DIR / 'staticfiles'.

Quick Troubleshooting Tips

  • If static files aren’t loading: Double-check STATIC_URL in settings and that publish in netlify.toml points to staticfiles.
  • If the app throws errors: Verify all environment variables are set correctly in Netlify—missing SECRET_KEY or DATABASE_URL are common culprits.
  • For database issues: Make sure your remote database is accessible from Netlify, and add python manage.py migrate to the build command in netlify.toml if you need to run migrations during deploy.

内容的提问来源于stack exchange,提问作者Rasmus Kierudsen

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 08:27:01