views:

199

answers:

3

Consider that I include namespaced reusable application:

urlpatterns = patterns('',
    # ella urls
    url('^ella/', include('ella.core.urls', namespace="ella")),
)

Now, the Ella applications has urls like that:

urlpatterns = patterns( '',
    url( r'^(?P<category>[a-z0-9-/]+)/$', category_detail, name="category_detail" ),
    # object detail
    url( r'^(?P<category>[a-z0-9-/]+)/(?P<year>\d{4})/(?P<month>\d{1,2})/(?P<day>\d{1,2})/(?P<content_type>[a-z0-9-]+)/(?P<slug>[a-z0-9-]+)/$',
        object_detail, name="object_detail" )
)

Now, calling {% url ella:category_detail category="cat" %} works fine. However, when object tries to generate link to it's details, it calls

from django.core.urlresolvers import reverse
url = reverse('object_detail', kwargs={'required' : 'params'})

This is not working, unless rewritten as

from django.core.urlresolvers import reverse
url = reverse('ella:object_detail', kwargs={'required' : 'params'})

So, if I understand it correctly, including reusable application into namespace breaks all inner reverse()s inside given application.

Is it true? What have I missed? Is there any way around?

A: 

Since you have name-spaced url configuration, you need to mention namespace:view-name pattern in order to reverse it properly (especially from view).

But, if you want to avoid this, you may also pass namespace/appname as current_app parameter. There are multiple ways to specify current_app when you are in template. But if you are in view, you need to hard-code as you did, or pass to current_app parameter

url = reverse('object_detail', 
              kwargs={'foo':'bar'}, 
              current_app=app_name_or_name_space)

refer: http://docs.djangoproject.com/en/dev/topics/http/urls/#reverse

Narendra Kamma
I think the real question is: is there a way to include a third party reusable app with a URL namespace and not need to rewrite all the calls to the reverse() function inside the reusable app.
moberley
Yup, exactly. current_app is same hardcode as prefix:...but thanks for the trick.
Almad
Agreed. But, How to get the current app name? is there such, otherwise how really pluggable apps work.
Narendra Kamma
A: 

URL Namespaces can be specified in two ways.

Firstly, you can provide the application and instance namespace as arguments to include() when you construct your URL patterns. For example,:

(r'^help/', include('apps.help.urls', namespace='foo', app_name='bar')),

This is right from http://docs.djangoproject.com/en/dev/topics/http/urls/#defining-url-namespaces.

Try changing include('ella.core.urls', namespace="ella") to include('ella.core.urls', namespace="ella", app_name="ella"). I'm not 100% this will work, but its worth a shot.

Scott
A: 

Rather than trying to namespace it all, could the urlconf option help to solve this? eg:

reverse('object_detail', urlconf='ella.core.urls')
John Mee