Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#27411 closed Bug (invalid)

Django namespaced reverse not working in test environment

Reported by: atkawa7 Owned by: nobody
Component: Testing framework Version: 1.10
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Marten Kenbeek)

I am have the following error when testing an app

ERROR: test_registration (accounts.tests.test_models.AccountTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\username\Downloads\djangoworkspace\src\accounts\tests\test_models.py", line 41, in test_registration
    url, user, format='json')
  File "C:\Python35\lib\site-packages\rest_framework\test.py", line 172, in post
    path, data=data, format=format, content_type=content_type, **extra)
  File "C:\Python35\lib\site-packages\rest_framework\test.py", line 94, in post
    return self.generic('POST', path, data, content_type, **extra)
  File "C:\Python35\lib\site-packages\django\test\client.py", line 387, in generic
    parsed = urlparse(force_str(path))
  File "C:\Python35\lib\site-packages\django\utils\encoding.py", line 76, in force_text
    s = six.text_type(s)
  File "C:\Python35\lib\site-packages\django\utils\functional.py", line 116, in __text_cast
    return func(*self.__args, **self.__kw)
  File "C:\Python35\lib\site-packages\django\urls\base.py", line 91, in reverse
    return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))
  File "C:\Python35\lib\site-packages\django\urls\resolvers.py", line 392, in _reverse_with_prefix
    (lookup_view_s, args, kwargs, len(patterns), patterns)
django.urls.exceptions.NoReverseMatch: Reverse for 'registration' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []

Here is my urls.py

   urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^accounts/', include('rest_framework.urls', namespace='rest_framework')),
    url(r'^api/v1/accounts/login/', obtain_auth_token, name='obtain_auth_token'),
    url(r'^api/v1/accounts/', include('accounts.urls', namespace='accounts')),
    url(r'', include('app.urls', namespace='app')),
]

accounts urls.py

from django.conf.urls import url
from . import views
urlpatterns = [
    url(r'^register/$', views.RegistrationView.as_view(), name='registration'),
    url(r'^users/$', views.AdminCreateUserView.as_view(), name='users'),
    url(r'^groups/$', views.GroupsCreateView.as_view(), name='groups'),
]

here is my example test

from rest_framework import test, status

from django.core.urlresolvers import reverse, reverse_lazy
from django.contrib.auth.models import User


class AccountTestCase(test.APITestCase):

    def test_registration(self):
        url = reverse_lazy('registration', current_app='accounts')
        user = {
            'username': 'John',
            'first_name': 'John',
            'last_name': 'Doe',
            'password': 'johnDoe12345*#$',
            'email': 'johndoe@example.com'
        }
        user2 = {
            'username': 'John2',
            'first_name': 'John',
            'last_name': 'Doe',
            'password': 'johnDoe12345*#$',
            'email': 'johndoe@example.com'
        }
        user3 = {
            'username': 'John2',
            'first_name': 'John',
            'last_name': 'Doe',
            'email': 'johndoe@example.com'
        }
        user4 = {
            'username': 'John4',
            'first_name': 'John',
            'last_name': 'Doe',
            'email': 'johndoe@example.com',
            'password': 'johnDoe12345*#$',
            'is_staff': True,
            'is_superuser': True
        }
        response = self.client.post(
            url, user, format='json')

However when I change urls.py to

   urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^accounts/', include('rest_framework.urls', namespace='rest_framework')),
    url(r'^api/v1/accounts/login/', obtain_auth_token, name='obtain_auth_token'),
    url(r'^api/v1/accounts/', include('accounts.urls')),
    url(r'', include('app.urls', namespace='app')),
]

It works fine.

Attachments (1)

django_bug.PNG (8.7 KB ) - added by atkawa7 8 years ago.

Download all attachments as: .zip

Change History (5)

by atkawa7, 8 years ago

Attachment: django_bug.PNG added

comment:1 by atkawa7, 8 years ago

Description: modified (diff)

comment:2 by atkawa7, 8 years ago

Component: UncategorizedTesting framework

comment:3 by Tim Graham, 8 years ago

Description: modified (diff)
Resolution: needsinfo
Status: newclosed

I don't think there's enough details here to determine what the issue is. In a case like this, it really helps to provide a complete sample project.

Please try our support channels first and reopen with details if it turns out to be a bug in Django. The minimal example to reproduce should ideally not include third-party apps such as django-rest-framework.

comment:4 by Marten Kenbeek, 8 years ago

Description: modified (diff)
Resolution: needsinfoinvalid

The correct way to reverse that url is reverse_lazy('accounts:registration'). The current_app parameter has no effect unless you also supply a namespace using the <namespace>:<viewname> syntax.

Be sure to read Reversing namespaced urls.

Note: See TracTickets for help on using tickets.
Back to Top