Python、Django、Django-cms升级顺序及指定版本升级工具咨询
Hey there! Let's walk through your upgrade plan step by step—this is a common jump from Python 2.7-era stacks, and following the right order will save you a lot of frustration. Here's what you need to know:
The golden rule here is transition incrementally, and move from "low-level" to "high-level" components once each layer is stable. For your specific stack, we can't jump straight from Python 2.7 to 3.7 with your current Django/Django CMS versions—we need intermediate steps to avoid breaking everything at once.
Your target is Python 3.7 → Django 2.2 → Django CMS 3.6. Here's the safest path:
1. First, Prepare Your Safety Net
- Backup everything: Tag your code repo, export a full database dump, and make sure you can roll back to your current working state at any time.
- Set up an isolated test environment: Use
virtualenvorvenvto create a copy of your production stack—never test upgrades directly on live code.
2. Transition to Python 3-compatible base versions (still on Python 2.7)
Directly jumping from Python 2.7 to 3.7 is risky. First, get your project running on the latest Python 2.7-compatible versions of Django and Django CMS that also support Python 3:
- Upgrade Django to 1.11.x (LTS): This is the last Django version that supports Python 2.7, and it also works with Python 3.5-3.7. Install it with
pip install django==1.11.29(grab the latest 1.11 stable release). Runpython manage.py checkandpython manage.py migrate, fixing any deprecation warnings or errors that pop up. - Upgrade Django CMS to 3.4.x latest: Django CMS 3.4 supports Django 1.8-1.11. Install the newest 3.4 release (e.g.,
pip install django-cms==3.4.6), run checks and migrations, and confirm your project still runs smoothly on Python 2.7.
3. Upgrade Python to 3.7
Now that your stack is Python 3-ready:
- Create a new Python 3.7 virtual environment:
python3.7 -m venv venv37, activate it, and reinstall your dependencies. Note: Some Python 2-only packages (likeMySQL-python) need replacement—swap them with Python 3 alternatives (e.g.,mysqlclient). - Use
2to3to auto-fix Python 2 → 3 syntax: Run2to3 -w your_project_rootto handle common issues likeprintstatements,unicode/strdifferences, andxrange→rangechanges. - Manual cleanup: Fix remaining issues like
iteritems()→items(),except Exception, e→except Exception as e, and encoding-related bugs. Run your project and test suite to catch any edge cases.
4. Upgrade Django to 2.2 (LTS)
Django 2.2 is fully compatible with Python 3.7 and is a long-term support release:
- Review Django's upgrade docs for 2.0-2.2 to spot deprecated APIs: Key changes include replacing
url()withpath()/re_path(), mandatoryon_deleteparameters for foreign keys, and moved modules likedjango.core.urlresolvers→django.urls. - Install Django 2.2:
pip install django==2.2.28(latest stable 2.2 release). - Run
python manage.py check --deployandpython manage.py migrate, fixing all deprecations. Tools likedjango-upgradecan automate many of these fixes for you.
5. Upgrade Django CMS to 3.6
Django CMS 3.6 supports Django 2.0-2.2 and Python 3.5-3.7, matching your target stack:
- Install Django CMS 3.6:
pip install django-cms==3.6.0(or the latest 3.6.x release). - Run migrations:
python manage.py migrateto handle any database schema changes from Django CMS. - Test custom plugins and templates: Some older plugin patterns or template tags might break in 3.6. Test every page and component to fix frontend/backend compatibility issues.
Static analysis and automation tools will save you hours of manual work:
- 2to3: Python's official tool for converting Python 2 code to Python 3 syntax—handles most routine changes automatically.
- django-upgrade: A Django-specific tool that fixes deprecated API calls (like replacing old
url()patterns, addingon_deleteparameters) across your codebase. - pyupgrade: Upgrades Python syntax to modern standards (e.g., f-strings instead of
str.format(),pathliboveros.path). - pylint-django: A Pylint plugin that checks for Django-specific code issues, including version compatibility warnings, to catch potential problems early.
内容的提问来源于stack exchange,提问作者Shankar Thiyagaraajan

