How to override Django view with name expansion url pattern?

I am using an external application called foo

which I like best, but I need to extend the functionality of one specific kind called Bar

. Usually I would just add my extended view to the urls.py

do include('foo.urls')

and call it the same, so hit it first.

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

      

The problem is that it foo

uses URLs with names everywhere, so the view in question is actually called foo:bar

, and the declaration include()

above should be:

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

      

This seems to work great for reanimated applications, but I'm having a hard time finding a way to override this view in my project. Is there any means of doing this without rewriting all calls to reverse()

and uses {% url %}

in foo

? This would, in part, mean unlocking the app and losing the purpose of reusing the view in the first place.

+1


source to share


1 answer


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/

. This url will then match your url pattern. The URL pattern name of your custom view shouldn't matter.



It won't work if the original application uses a different url than path_to_bar

. In this case, you still don't need to change all the calls to reverse

- just create your own my_foo_urls.py

, duplicate the content, foo/urls.py

and include that.

+1


source







All Articles