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

Django生产部署后template路径错误及Admin样式失效问题

Hey there, let's work through your Django deployment issues step by step—first fixing the template path problem, then getting the Admin's CSS/JS back up and running. Here's what you need to do:

1. Correctly Configure Template Paths

Moving your templates folder to /var/www worked as a quick fix because Django checks system-level template directories by default, but that's not the cleanest or most maintainable approach. Instead, you should explicitly tell Django where your project's templates live in your settings.py:

First, confirm your project structure (it should look something like this):

your_project/
├── your_project/
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── templates/
│   ├── your_app_templates/
│   └── (any custom admin templates if you have them)
└── manage.py

Open your_project/settings.py and update the TEMPLATES section to include your project's template directory using BASE_DIR (which Django defines as your project's root folder):

import os

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        # Add this line to point to your project's templates folder
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

This tells Django to look for templates in the templates folder at your project's root. You can now move your templates folder back from /var/www to your project's root directory, where it belongs.

2. Fix Django Admin's Missing Static Files

The Admin's CSS/JS breaking happens because Django doesn't serve static files in production when DEBUG = False. You need to collect all static files into a single directory and configure your web server (like Nginx or Apache) to serve them.

Step 1: Update Static File Settings in settings.py

Add or modify these lines in settings.py:

STATIC_URL = '/static/'
# This is where Django will collect all static files
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

STATIC_ROOT can be any absolute path—using os.path.join(BASE_DIR, 'staticfiles') keeps it within your project, which is convenient.

Step 2: Collect Static Files

Run this command in your project's root directory to copy all static files (including Admin's CSS/JS) into the STATIC_ROOT folder:

python manage.py collectstatic

You'll see a prompt confirming the files will be copied—type yes to proceed.

Step 3: Configure Your Web Server to Serve Static Files

If you're using Nginx, add this location block to your server configuration:

location /static/ {
    alias /path/to/your/project/staticfiles/; # Replace with your actual STATIC_ROOT path
    expires 30d; # Caches static files for better performance
}

If you're using Apache, you'd use an Alias directive like this:

Alias /static/ /path/to/your/project/staticfiles/
<Directory /path/to/your/project/staticfiles/>
    Require all granted
</Directory>

After updating your web server config, restart it to apply the changes.

Final Checks
  • Make sure DEBUG = False in your production settings.py (never run production with DEBUG = True—it's a security risk).
  • Double-check all file permissions: your web server user (like www-data for Nginx/Apache) should have read access to both your templates and staticfiles directories.

That should resolve both your template path issue and the missing Admin styles!

内容的提问来源于stack exchange,提问作者Kanagawa Marcos

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 08:01:40