Last Updated: February 25, 2016
·
1.312K
· avanov

Pyramid-like view configuration in Django

Recently I've started a new project that allows us to use Pyramid-like view configuration in Django - https://github.com/avanov/Rhetoric

If you ever had a chance to implement a pyramid-based web app, you might have noticed how flexible its URL Dispatcher becomes [when you have to deal with RESTful endpoints][1]. Now with Rhetoric, the same functionality is available in Django.

The project is still in early development, but already provides a complete pattern matching syntax and a method-based URL dispatcher.

Example:

# project_name.urls
from django.conf.urls import patterns, include, url
# ... other imports ...
from rhetoric import Configurator

# ... various definitions ...

urlpatterns = patterns('',
    # ... a number of standard django url definitions ...
)

# Rhetorical routing
# ------------------
config = Configurator()
config.add_route('test.new.routes', '/test/new/routes/{param:[a-z]+}')
config.scan(ignore=[
    # do not scan test modules included into the project tree
    re.compile('^.*[.]?tests[.]?.*$').match,
    # do not scan settings modules
    re.compile('^settings[_]?[_a-z09]*$').match,
])
urlpatterns.extend(config.django_urls())
# project_name.some_app.some_module

from rhetoric import view_config


@view_config(route_name="test.new.routes", renderer='json')
def view_get(request, param):
    return {
        'Hello': param
    }

@view_config(route_name="test.new.routes", renderer='json', request_method='POST')
def view_post(request, param):
    return {
        'Hello': 'POST'
    }

[1]: http://blog.delaguardia.com.mx/pyramid-view-configuration-let-me-count-the-ways.html

2 Responses
Add your response

So, now when someone looks at your Django project expecting well known Django configuration, they'll be left thinking..

"Uh, wat?!"

Now, supplementing Django's routing with Pyramid's traversal routing system.. That's an entirely different conversation altogether :)

over 1 year ago ·

Well, you are right.
On the other hand, I couldn't say that the "idiomatic" Django configuration is good enough to follow it in every project. I would rather say that the only benefit of it is that it's standard. And I'd really like to add traversal routing to the project someday, just as a working option - in the same manner as Pyramid Hybrid Routing works (which is not a desired approach in most cases, but could be a salvation to developers working on some specific features that require complex view lookup logic).

over 1 year ago ·