3

I'm using external application called foo which I mostly like, but I need to extend functionality of one specific view called Bar. Normally, I'd just put my extended view in urls.py before include('foo.urls') and name it the same, so that resolver hits it first:

urlpatterns = [
    ...
    url(r'^foo/path_to_bar$', CustomBar.as_view(), name='bar'),
    url(r'^foo/$', include('foo.urls')),
    ...
]

The problem is, foo uses namespaced urls everywhere, so the view in question is actually referred to by foo:bar and the include() declaration above should actually be:

include('foo.urls', namespace='foo', app_name='foo')

This is apparently great for resuable apps, but I have hard time finding the way to override this view in my project. Are there any means to do so, without rewriting all calls to reverse() and uses of {% url %} in foo? That would esentially amount to forking the app and defeat the purpose of reusing a view in the first place.

Red
  • 1,450
  • 2
  • 17
  • 33

1 Answers1

3

I'm not sure I understand the problem. {% url 'foo:foo' %} will match the url pattern in the app, and return /foo/path_to_bar/. That URL will then be matched by your custom URL pattern. The name of the URL pattern of your custom view shouldn't matter.

That won't work if the original app uses a different URL than path_to_bar. In this case you still don't have to change all the calls to reverse - just create your own my_foo_urls.py, duplicate the contents of foo/urls.py, and include that instead.

Alasdair
  • 298,606
  • 55
  • 578
  • 516