Upgrading Django 3.2 to Django 4.2

All good things come to an end. Version 4.2 is the new Django LTS-version (Long Term Support) for a popular Python web framework. This is how you can upgrade your project handily with django-upgrade.

Installing Django-upgrade

Install it with pip straight away, or add it to your requirements.txt. Use a virtual environment to be safe and sane.

sudo apt-get install virtualenv

Start up the env.

source env/bin/activate

Install django-upgrade.

pip install django-upgrade

I prefer the requirements.txt-route:

requirements.txt

django===4.2.1
django-upgrade
pip install -r requirements.txt

Using Django-upgrade

Use the command:

django-upgrade --target-version 4.2.1 example/core/models.py example/settings.py

In my case, I needed to upgrade my urls.

django-upgrade --target-version 4.2.1 pathto/urls.py

Worked like a charm and fixed paths and static files on one go!

Verify results

To verify the results, I checked that my setup worked and my dev-server was running great locally and then yolo-pushed the changes to my production server, since it's just this site and it has nothing that a database restore + git can't change back to way it was.

django-admin --version

Actually I had some worries regarding MarkdownX, so I had to mod it's paths a bit to comply with Django 4.2 syntax. Here's the updated urls.py of that plugin for anyone experiencing similar woes:

"""
**MarkdownX** default URLs, to be added to URLs in the main project.

See URLs in :doc:`../../example` to learn more.
"""

from django.urls import path

from .views import (
    ImageUploadView,
    MarkdownifyView,
)


urlpatterns = [
    path('upload/', ImageUploadView.as_view(), name='markdownx_upload'),
    path('markdownify/', MarkdownifyView.as_view(), name='markdownx_markdownify'),
]

Source(s)

  1. Django-upgrade on Github

All articles


Comments

No published comments yet.


Add a Comment

Your comment is held for review before it appears publicly.

Your email is never published or shared.

Up to 2,000 characters.

Tick the box, then click Submit right away (Google expires the check after about two minutes).