Testing Redirects

A snippet showing how to test redirects in Django.


# models.py

from django.db import models

class Gate(models.Model):
    is_open = models.BooleanField(default=False)
      

# admin.py

from django.contrib import admin
from .models import Gate

@admin.register(Gate)
class GateAdmin(admin.ModelAdmin):
    list_display = ("is_open",)
      

# Result (tests/test_redirects.py)
from django.test import TestCase
from django.urls import reverse

class RedirectTest(TestCase):
    def test_redirect(self):
        res = self.client.get(reverse("go-somewhere"))
        self.assertEqual(res.status_code, 302)
        self.assertRedirects(res, "/target/")
      
Explanation:
  • Use assertRedirects(response, expected_url) for robust redirect assertions.
  • Also check status code (301/302) when needed.
  • Category Testing
  • Total Views 422
  • Last Modified 09 March, 2026
  • Tags #testing #redirects #views #client
Never miss a story on Django.wiki

Subscribe for fresh tutorials, snippets, and updates.

By subscribing you agree to our Privacy Policy.