﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
20479	Adding the concept predicates in the Django url routing	rach	nobody	"It's something that I tried to bring back from Pyramid.
It's not as complete because the url routing and view lookup is made in one step in django.
But that allow to add simple matching rules in the logic of the UrlResolver

See pull-request : https://github.com/django/django/pull/1199

It includes Code, Doc, Tests. 

The motivation behind this feature is being able to have more flexibility in the way to organize 
function based views. And specially getting rid of redundant test.

Use cases: 

- Post multi forms on a single views, this allow to match the view which match when a specific submit is in the request

- Get rid of redundant `if method == 'POST'` in function base view

{{{
#!div style=""font-size: 80%""
Code highlighting:
  {{{#!python
      
        url_pattern += url('/', my_view_GET)
        url_pattern += url('/', my_view_POST)

        #Decorator to make a urls match only if the predicates return True

        predicate_GET = lambda request: request.method == 'GET'
        predicate_POST = lambda request: request.method == 'POST'

        @url_predicates([predicate_GET])
        def my_view_GET(request):
            # I can assume now that only GET requests get match to the url
            # associated to this view
            # ...

        @url_predicates([predicate_POST])
        def my_view_POST(request):
            # I can assume now that only POST requests get match to the url
            # associated to this view
            # ...

  }}}
}}}



....

The implementation is probably imperfect let me know your feedbacks.
But I hope that could end with a more flexible and powerful routing in Django. 

The Pyramid documentation illustrate very well the configuration possibility of predicates :
http://docs.pylonsproject.org/projects/pyramid/en/latest/narr/viewconfig.html#predicate-arguments
 
"	New feature	closed	Core (URLs)	dev	Normal	wontfix	urlresolver, predicates	marc.tamlyn@…	Someday/Maybe	1	0	0	0	0	0
