Django Admin集成CKEditor后本地正常、Apache2部署时仅显示textarea的问题求助
Hey there, let's work through this CKEditor issue you're hitting. When your Django app runs locally with python manage.py runserver, everything works because the dev server automatically serves static files—but Apache needs explicit configuration to handle them, which is almost always the root cause here. Let's break down the steps to fix this:
1. Make sure you've collected all static files
Django doesn't automatically serve static files in production mode, so you need to gather all static assets (including CKEditor's files) into a single directory using the collectstatic command:
python manage.py collectstatic
- First, double-check your
settings.pyhas the correct static file settings:STATIC_URL = '/static/' # Use an absolute path here, e.g., for a project at /var/www/my_django_app/ STATIC_ROOT = '/var/www/my_django_app/static/' - After running
collectstatic, verify that theckeditorfolder exists inside yourSTATIC_ROOTdirectory—this should contain all the JS, CSS, and other assets CKEditor needs.
2. Configure Apache to serve static files
You need to tell Apache where your static files live so it can serve them to visitors. Edit your Apache virtual host configuration file (usually located at /etc/apache2/sites-available/your_site.conf) and add these lines:
# Serve static files Alias /static/ /var/www/my_django_app/static/ <Directory /var/www/my_django_app/static/> Require all granted Options -Indexes </Directory>
- Replace
/var/www/my_django_app/static/with your actualSTATIC_ROOTpath. - Ensure Apache has permission to read this directory:
sudo chown -R www-data:www-data /var/www/my_django_app/static/ sudo chmod -R 755 /var/www/my_django_app/static/
3. Verify CKEditor's base path configuration
Sometimes CKEditor can't find its assets if the base path isn't set correctly in production. Add this to your settings.py:
CKEDITOR_BASEPATH = STATIC_URL + 'ckeditor/ckeditor/'
This ensures CKEditor looks for its files in the correct static directory served by Apache.
4. Check for 404 errors in the browser
Open your browser's developer tools (F12 → Console tab) and reload the Admin page. If you see 404 errors for CKEditor's JS/CSS files, that confirms the static files aren't being served properly. Go back to steps 1 and 2 to fix the path or Apache configuration.
5. Restart Apache after changes
Don't forget to apply your Apache configuration changes by restarting the service:
sudo systemctl restart apache2
6. Ensure DEBUG is set to False
In production, your settings.py should have DEBUG = False—when DEBUG is True, Django might handle static files differently, but it's not safe for production. If you left DEBUG on, switch it off and re-run collectstatic just to be sure.
Most of the time, running collectstatic correctly and configuring Apache's static file alias fixes this issue. Start with those two steps, and you should see CKEditor load up in the Admin panel shortly.
内容的提问来源于stack exchange,提问作者sandeep kumar

