Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #2370 -- It's now possible to pass default URLconf arguments to…

… include(). Added docs, as well. Thanks for the patch, martin.glueck@gmail.com

git-svn-id: http://code.djangoproject.com/svn/django/trunk@3506 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 1a428ec9b8efd24b0ced665d348160a9fd396c42 1 parent 0f977e9
Adrian Holovaty authored August 01, 2006
1  AUTHORS
@@ -70,6 +70,7 @@ answer newbie questions, and generally made Django that much better:
70 70
     Clint Ecker
71 71
     gandalf@owca.info
72 72
     Baishampayan Ghose
  73
+    martin.glueck@gmail.com
73 74
     Espen Grindhaug <http://grindhaug.org/>
74 75
     Brant Harris
75 76
     hipertracker@gmail.com
8  django/conf/urls/defaults.py
@@ -10,8 +10,10 @@
10 10
 def patterns(prefix, *tuples):
11 11
     pattern_list = []
12 12
     for t in tuples:
13  
-        if type(t[1]) == list:
14  
-            pattern_list.append(RegexURLResolver(t[0], t[1][0]))
  13
+        regex, view_or_include = t[:2]
  14
+        default_kwargs = t[2:]
  15
+        if type(view_or_include) == list:
  16
+            pattern_list.append(RegexURLResolver(regex, view_or_include[0], *default_kwargs))
15 17
         else:
16  
-            pattern_list.append(RegexURLPattern(t[0], prefix and (prefix + '.' + t[1]) or t[1], *t[2:]))
  18
+            pattern_list.append(RegexURLPattern(regex, prefix and (prefix + '.' + view_or_include) or view_or_include, *default_kwargs))
17 19
     return pattern_list
6  django/core/urlresolvers.py
@@ -130,12 +130,13 @@ def reverse_helper(self, *args, **kwargs):
130 130
         return reverse_helper(self.regex, *args, **kwargs)
131 131
 
132 132
 class RegexURLResolver(object):
133  
-    def __init__(self, regex, urlconf_name):
  133
+    def __init__(self, regex, urlconf_name, default_kwargs=None):
134 134
         # regex is a string representing a regular expression.
135 135
         # urlconf_name is a string representing the module containing urlconfs.
136 136
         self.regex = re.compile(regex)
137 137
         self.urlconf_name = urlconf_name
138 138
         self.callback = None
  139
+        self.default_kwargs = default_kwargs or {}
139 140
 
140 141
     def resolve(self, path):
141 142
         tried = []
@@ -149,7 +150,8 @@ def resolve(self, path):
149 150
                     tried.extend([(pattern.regex.pattern + '   ' + t) for t in e.args[0]['tried']])
150 151
                 else:
151 152
                     if sub_match:
152  
-                        return sub_match[0], sub_match[1], dict(match.groupdict(), **sub_match[2])
  153
+                        sub_match_dict = dict(self.default_kwargs, **sub_match[2])
  154
+                        return sub_match[0], sub_match[1], dict(match.groupdict(), **sub_match_dict)
153 155
                     tried.append(pattern.regex.pattern)
154 156
             raise Resolver404, {'tried': tried, 'path': new_path}
155 157
 
42  docs/url_dispatch.txt
@@ -389,3 +389,45 @@ to pass metadata and options to views.
389 389
 
390 390
 .. _generic views: http://www.djangoproject.com/documentation/generic_views/
391 391
 .. _syndication framework: http://www.djangoproject.com/documentation/syndication/
  392
+
  393
+Passing extra options to ``include()``
  394
+--------------------------------------
  395
+
  396
+**New in the Django development version.**
  397
+
  398
+Similarly, you can pass extra options to ``include()``. When you pass extra
  399
+options to ``include()``, *each* line in the included URLconf will be passed
  400
+the extra options.
  401
+
  402
+For example, these two URLconf sets are functionally identical:
  403
+
  404
+Set one::
  405
+
  406
+    # main.py
  407
+    urlpatterns = patterns('',
  408
+        (r'^blog/', include('inner'), {'blogid': 3}),
  409
+    )
  410
+
  411
+    # inner.py
  412
+    urlpatterns = patterns('',
  413
+        (r'^archive/$', 'mysite.views.archive'),
  414
+        (r'^about/$', 'mysite.views.about'),
  415
+    )
  416
+
  417
+Set two::
  418
+
  419
+    # main.py
  420
+    urlpatterns = patterns('',
  421
+        (r'^blog/', include('inner')),
  422
+    )
  423
+
  424
+    # inner.py
  425
+    urlpatterns = patterns('',
  426
+        (r'^archive/$', 'mysite.views.archive', {'blogid': 3}),
  427
+        (r'^about/$', 'mysite.views.about', {'blogid': 3}),
  428
+    )
  429
+
  430
+Note that extra options will *always* be passed to *every* line in the included
  431
+URLconf, regardless of whether the line's view actually accepts those options
  432
+as valid. For this reason, this technique is only useful if you're certain that
  433
+every view in the the included URLconf accepts the extra options you're passing.

0 notes on commit 1a428ec

Please sign in to comment.
Something went wrong with that request. Please try again.