Adding the concept predicates in the Django url routing
|Reported by:||rach||Owned by:||nobody|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
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.
- 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
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 :
Change History (4)
comment:1 Changed 23 months ago by svisser
- Needs documentation unset
- Needs tests unset
- Patch needs improvement unset
comment:2 Changed 23 months ago by mjtamlyn
- Cc marc.tamlyn@… added
- Has patch set
- Triage Stage changed from Unreviewed to Someday/Maybe