请教:如何在Netlify上部署cookiecutter-django或Django项目?
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(orpyproject.tomlfor Poetry,Pipfilefor 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
functionsfolder in your project root. - Inside that folder, make a file called
django_app.pywith 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 Djangosettings.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:
- Go to your Netlify site’s Site Settings > Environment Variables
- Add variables like:
SECRET_KEY: Your Django secret keyDATABASE_URL: URL for your remote database (Netlify doesn’t host databases—use something like ElephantSQL or AWS RDS)DEBUG: Set toFalsefor productionALLOWED_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.productionas yourDJANGO_SETTINGS_MODULEindjango_app.py(that’s the default production config). - Ensure your
ALLOWED_HOSTSinconfig/settings/production.pyincludes your Netlify URL. - The default
collectstaticsetup should work, but confirmSTATIC_ROOTis set toBASE_DIR / 'staticfiles'.
Quick Troubleshooting Tips
- If static files aren’t loading: Double-check
STATIC_URLin settings and thatpublishinnetlify.tomlpoints tostaticfiles. - If the app throws errors: Verify all environment variables are set correctly in Netlify—missing
SECRET_KEYorDATABASE_URLare common culprits. - For database issues: Make sure your remote database is accessible from Netlify, and add
python manage.py migrateto thebuildcommand innetlify.tomlif you need to run migrations during deploy.
内容的提问来源于stack exchange,提问作者Rasmus Kierudsen

