Let's say you make a post about your favorite color.

/red-is-my-favorite-color

Time passes, and you decide red isn't so hot anymore. Now your favorite color is blue. So you change the slug.

/blue-is-my-favorite-color

But now everyone who visits the original link gets a 404. To fix that up, I followed the Django Docs and added the redirects app to my Django project in this commit. as of July 2022, all you have to do is:

  1. Ensure that the django.contrib.sites framework is installed.
  2. Add 'django.contrib.redirects' to your INSTALLED_APPS setting.
  3. Add 'django.contrib.redirects.middleware.RedirectFallbackMiddleware' to your MIDDLEWARE setting.
  4. Run the command manage.py migrate.

Now on your admin page, you can add all the redirects you want

screenshot of redirect page showing /red-is-my-favorite-color redirecting to /blue-is-my-favorite-color

I could add something in signals.py to do this automatically on slug change, but I don't want this behavior to be automagic, and I don't change link slugs very often.

Hope that was useful to you 😁

Comments

Back to Home
John Solly Profile Picture
John Solly Profile Picture

John Solly

Hi, I'm John, a Software Engineer with a decade of experience building, deploying, and maintaining cloud-native geospatial solutions. I currently serve as a senior software engineer at HazardHub (A Guidewire Offering), where I work on a variety of infrastructure and application development projects.

Throughout my career, I've built applications on platforms like Esri and Mapbox while also leveraging open-source GIS technologies such as OpenLayers, GeoServer, and GDAL. This blog is where I share useful articles with the GeoDev community. Check out my portfolio to see my latest work!