mirror of
				https://github.com/django/django.git
				synced 2025-10-31 09:41:08 +00:00 
			
		
		
		
	[1.5.x] Fixed #21137 -- Documented best practice for URLconfs with repeated pattern prefixes.
Backport of 222460a994 from master
			
			
This commit is contained in:
		
				
					committed by
					
						 Tim Graham
						Tim Graham
					
				
			
			
				
	
			
			
			
						parent
						
							b8e7730f3e
						
					
				
				
					commit
					61b685847e
				
			| @@ -366,6 +366,32 @@ instead. For example, consider this URLconf:: | ||||
| In this example, the ``/credit/reports/`` URL will be handled by the | ||||
| ``credit.views.report()`` Django view. | ||||
|  | ||||
| This can be used to remove redundancy from URLconfs where a single pattern | ||||
| prefix is used repeatedly. For example, consider this URLconf:: | ||||
|  | ||||
|     from django.conf.urls import patterns, url | ||||
|  | ||||
|     urlpatterns = patterns('wiki.views', | ||||
|         url(r'^(?P<page_slug>\w+)-(?P<page_id>\w+)/history/$', 'history'), | ||||
|         url(r'^(?P<page_slug>\w+)-(?P<page_id>\w+)/edit/$', 'edit'), | ||||
|         url(r'^(?P<page_slug>\w+)-(?P<page_id>\w+)/discuss/$', 'discuss'), | ||||
|         url(r'^(?P<page_slug>\w+)-(?P<page_id>\w+)/permissions/$', 'permissions'), | ||||
|     ) | ||||
|  | ||||
| We can improve this by stating the common path prefix only once and grouping | ||||
| the suffixes that differ:: | ||||
|  | ||||
|     from django.conf.urls import include, patterns, url | ||||
|  | ||||
|     urlpatterns = patterns('wiki.views', | ||||
|         url(r'^(?P<page_slug>\w+)-(?P<page_id>\w+)/', include(patterns('', | ||||
|             url(r'^history/$', 'history'), | ||||
|             url(r'^edit/$', 'edit'), | ||||
|             url(r'^discuss/$', 'discuss'), | ||||
|             url(r'^permissions/$', 'permissions'), | ||||
|         ))), | ||||
|     ) | ||||
|  | ||||
| .. _`Django Web site`: https://www.djangoproject.com/ | ||||
|  | ||||
| Captured parameters | ||||
|   | ||||
		Reference in New Issue
	
	Block a user