Django reverse lookup fails during unit testing

100 views
Skip to first unread message

Derek

unread,
Mar 16, 2021, 2:04:33 PM3/16/21
to django-users
Hi

When I open the Django shell (python manage.py shell) and test a reverse URL:

>>> from django.urls import reverse
>>> reverse('admin:locales_site_changelist')
'/admin/locales/site/'

then it works as expected.

However, when I try and use the same lookup string via a unit test:

from django.test import TestCase
from core.models import CustomUser as User

class TestCaseSimple(TestCase):

    def setUp(self):
        User.objects.create_superuser(
            email='te...@example.com',
            password='password')
        self.login = self.client.login(
            email='te...@example.com',
            password='password')
        print('Login successful? ', self.login)  # True
        url = reverse('admin:locales_site_changelist')


I get this error:        

E       django.urls.exceptions.NoReverseMatch: Reverse for 'locales_site_changelist' not found. 'locales_site_changelist' is not a valid view function or pattern name.


Is there some simple reason why reverse lookup does not work when testing?  (I actually need to use it in a client post request.)

Thanks
Derek

Reply all
Reply to author
Forward
0 new messages