Passing extra options to view functions

URLconfs have a hook that lets you pass extra arguments to your view functions, as a Python dictionary. The django.conf.urls.url() function can take an optional third argument which should be a dictionary of extra keyword arguments to pass to the view function. For example:

from django.conf.urls import url 
from . import views 
 
urlpatterns = [ 
    url(r'^reviews/(?P<year>[0-9]{4})/$',  
        views.year_archive,  
        {'foo': 'bar'}), 
] 

In this example, for a request to /reviews/2005/, Django will call views.year_archive(request, year='2005', foo='bar'). This technique is used in the syndication framework to pass metadata and options to views (see Chapter 14, Generating Non-HTML Content).

Note

Dealing with conflicts

It's possible ...

Get Mastering Django: Core now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.