I know this is a pretty old question... but I think I found a solution:
As Will Hardy suggested you'll have to keep app_name
the same for both instances (or not define it at all, it will default to the app the included urls reside in). Define a separate namespace for each app instance though:
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') ),
)
Now comes the slightly tricky part of setting the currently active app instance (namespace) in your views. Meaning you have to find out which app instance is active and pass it to RequestContext
.
To find the currently active app, django.urls.resolve
can be used:
r = resolve(request.path)
r.app_name # the app name
r.namespace # the the currently active instance
So you'll have to update your views (this assumes using the class based views) accordingly:
from django.urls import resolve
from django.views.generic import TemplateView
class AlbumCreateView(TemplateView):
template_name = 'path/to/my/template.html'
def render_to_response(self, context, **response_kwargs):
response_kwargs['current_app'] = resolve(self.request.path).namespace
return super(AlbumPageView, self).render_to_response(context, **response_kwargs)
Now the url tag will automatically reverse to the correct namespace and still allow reversing to a specific app namespace if needed:
{% url webui_photos:album-create %} {# returns the url for whatever app is current #}
{% url car-photos:album-create %}
{% url profile-photos:album-create %}
To reverse urls in views, the current app instance has to be passed in manually:
reverse('webui_photos:album-create', current_app=resolve(self.request.path).namespace))
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…