Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: How do I override app-supplied urls in my project urlconf?

Tags:

Let's say I have a Django project with three apps: foo, bar, and glue. I'm trying to follow reusable app conventions, so foo and bar are not dependent on (and don't know anything about) each other or on glue. Glue contains code to integrate the other two apps into the site.

Foo supplies a template tag that I want to include in one of the pages supplied by bar. The view for bar's page can be passed an alternate template. I make a template in glue that extends bar's template and includes the template tag from foo. In order to pass my new template to bar's view, I need to modify the urlconf entry that points to it.

My project urlconf looks something like this:

urlpatterns = patterns('',   (r'^$', include('glue.urls')),   (r'^foo/', include('foo.urls')),   (r'^bar/', include('bar.urls')), ) 

What's the most elegant way to pass the alternate template (or any other arbitrary view arguments, for that matter) to the view in bar? I don't want to modify bar's urlconf directly, as that would make it dependent on glue.

The only other method I can think of is to remove include('bar.urls'), copy the url patterns in bar's urlconf into the project urlconf, and modify the pattern I'm interested in. That approach violates the DRY principle though. Is there some other solution that I'm missing?

like image 444
Chris Acheson Avatar asked Feb 16 '12 05:02

Chris Acheson


People also ask

How can we handle URLs in Django?

Django runs through each URL pattern, in order, and stops at the first one that matches the requested URL, matching against path_info . Once one of the URL patterns matches, Django imports and calls the given view, which is a Python function (or a class-based view).

What is reverse URL in Django?

the reverse function allows to retrieve url details from url's.py file through the name value provided there. This is the major use of reverse function in Django. Syntax: Web development, programming languages, Software testing & others. from django.urls import reverse.

How do I add a URL to a Django project?

project/urls.py is the one that django uses, so you have to import to project/urls.py. Which means, that you have to state your imports in project/urls.py. The imported urls.py file must also define a valid urlconf, named urlpatterns. I think I had to restart the server (plus forgot the include), works now!

Is URL and path same in Django?

The path function is contained with the django. urls module within the Django project code base. path is used for routing URLs to the appropriate view functions within a Django application using the URL dispatcher.


1 Answers

Apparently duplicate URLs are allowed in the urlconf, and the first match listed will have priority:

urlpatterns = patterns('',   (r'^$', include('glue.urls')),   (r'^foo/', include('foo.urls')),    # This will override the same URL in bar's urlconf:   (r'^bar/stuff/$', 'glue.views.new_bar_stuff', {'arg': 'yarrgh'}, 'bar_stuff'),    (r'^bar/', include('bar.urls')), ) 
like image 134
Chris Acheson Avatar answered Sep 30 '22 16:09

Chris Acheson