Django url namespace reuse

I have a Django project, trainingand the application within this project tests. The folder structure is as follows:

django-training
    tests
        urls.py
    training
        urls.py

Inside training/urls.pyI defined this template:

url(r'^tests/', include('tests.urls', namespace='tests'))

And inside tests/urls.pyI have these templates defined:

url(r'^$', index, name='index'),
url(r'^(\d+)/$', view, name='view'),
url(r'^give-up/$', give_up, name='give_up'),
url(r'^(\d+)/result/$', result, name='result')

Everything is working fine.

But what if I want to pack the application testsas a reusable application that works in any Django project? What to do with URL patterns?

I created the file tests/settings.pyand changed the ROOT_URLCONFvar configuration to point to tests/urls.py. But this will not work, as this error will occur:

Traceback (most recent call last):
File "/home/user/.virtualenvs/clean2/local/lib/python2.7/site-packages/tests/tests.py", line 173, in testContext
    response = self.client.get(reverse('tests:view', args=(1,)))
File "/home/user/.virtualenvs/clean2/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 492, in reverse
key)
NoReverseMatch: u'tests' is not a registered namespace

, reverse , (tests, ).

: , URL- Django, ?

+4
2

Django.

tests/urls.py test :

test_patterns = patterns('',
    url(r'^$', index, name='index'),
    url(r'^(\d+)/$', view, name='view'),
    url(r'^give-up/$', give_up, name='give_up'),
    url(r'^(\d+)/result/$', result, name='result'),
)

urlpatterns = patterns('',
    url(r'^tests/', include(test_patterns, namespace='tests')),
)

URL , , .

+2

- , , tests's url urls.py, :

response = self.client.get(reverse('tests:view', args=(1,)))

:

u'tests' is not a registered namespace

tests urls.py.

+2

All Articles