views:

180

answers:

3

I'm writing reusable app. And I want to deploy it several times.

Here is urls.py:

urlpatterns = patterns('',
(r'^carphotos/', include('webui.photos.urls', app_name='car-photos') ),
(r'^userphotos/', include('webui.photos.urls',  app_name='profile-photos') ),)

and photos/urls.py:

urlpatterns = patterns('webui.photos.views',
url(r'^$', album_list, name="album-list" )
url(r'^newalbum/$', album_page, {'create': True}, name="album-create"),)

On the album_list view I want to show url for creating new album album_page.

I found that I have to use parameter current_app of reverse function to get proper URL.

But how to get this current_app? I thought the answer is something simple. But I can't find it in django documentation.

Thanks, Nick

A: 

I believe that's what the sites framework is for.

Hank Gay
I read docs you suggested. I don't think it's right answer. According to documentation Sites framework is useful when you want to share information between sites, but in my case there is one site only. Technically it is possible to deploy my photos app on different sub-domains, but I think its wrong to create separate domains for every instance of reusable app.
Nike
+1  A: 

In your urls, you have a different app_name even though it's the same app. Set the app_name to the same thing, and set namespace uniquely for each instance. eg.

urlpatterns = patterns('',
(r'^carphotos/', include('webui.photos.urls', app_name="webui_photos", namespace='car-photos') ),
(r'^userphotos/', include('webui.photos.urls', app_name="webui_photos", namespace='profile-photos') ),)

Then provide the current_app argument when using reverse. See http://docs.djangoproject.com/en/dev/topics/http/urls/#reverse and http://docs.djangoproject.com/en/dev/topics/http/urls/#url-namespaces

[edit] after re-reading your question:

you don't need to provide the current_app argument if you are using the {% url %} tag. As far as I'm lead to believe, it will automatically access a template variable called current_app, which is automatically set based on the matched url.

Will Hardy
I tried this code, and it doesn't solve the problem. As I found, current_app is the argument of Context or RequestContext which I have to provide explicitly. And how to get it is the question of this page. BTW after setting namespace argument, my urls become broken as described at http://code.djangoproject.com/ticket/11559
Nike
A: 

After exploring this topic for several days, I found that it isn't natural to mount django app more than once.

There is implementation of pluggable applications pattern: http://github.com/nowells/django-pluggables. It looks too tricky for me.

So I decided to move repeated functionality to custom tags and duplicate templates for each usage of my app. I hope using custom tags and extend feature help me to follow DRY principle.

Nike