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 😁

Back to Home
 Profile Picture
Profile Picture

About John Solly

I am a Senior Software Engineer with a focus on geospatial applications, based in the Columbus, OH metropolitan area. This blog is where I delve into the intricacies of GIS (Geographic Information Systems), offering deep dives into different components of the geospatial technology stack. For those who share a passion for GIS and its applications, you've found a spot to explore and learn.

Interested in collaborating or learning more about my work? Take a look at my portfolio for a showcase of my projects and expertise.

Comments

  • No comments yet.
Login to Comment