Opened 8 years ago
Last modified 8 years ago
#27411 closed Bug
Django namespaced reverse not working in test environment — at Initial Version
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
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')), ]
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.
Note:
See TracTickets
for help on using tickets.