-
Book Overview & Buying
-
Table Of Contents
Mastering Django: Core
By :
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).
Dealing with conflicts
It's possible to have a URL pattern which captures named keyword arguments, and also passes arguments with the same names in its dictionary of extra arguments. When this happens, the...
Change the font size
Change margin width
Change background colour