RSS DEV Community

Django Redirects App Tutorial

To set up Django redirects, you need to configure the Sites framework and the Redirects app in your Django project. First, add 'django.contrib.sites' and 'django.contrib.redirects' to your INSTALLED_APPS in settings.py. Then, set a SITE_ID and run a migration to update your database. For the Redirects app, add 'django.contrib.redirects.middleware.RedirectFallbackMiddleware' to your MIDDLEWARE list and run another migration. You can manage redirects through the Django admin interface, where you can add, edit, and view all existing redirects. Use absolute paths for "redirect from" and either absolute paths or full URLs for "redirect to." You can also change the site's domain name or display name in the Sites section of the admin. Django's redirects app uses permanent (301) redirects by default, which is good for SEO. Regularly review your redirects and monitor performance on your production website, and consider caching options if needed. Use relative URLs for new paths to ease transitions between environments, and add tests to confirm that redirects are working as expected.
dev.to
dev.to