Giter Site home page Giter Site logo

bmispelon / django-cbvpatterns Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mjtamlyn/django-cbvpatterns

0.0 2.0 0.0 92 KB

A nicer version of patterns() for use with class based views.

License: BSD 2-Clause "Simplified" License

Python 100.00%

django-cbvpatterns's Introduction

Django-cbvpatterns

A nicer version of patterns() for use with class based views. Inspired largely by Loic.

What is this?

If you're a big fan of class based views in Django, you might often find your urls.py starting to look a little cluttered. Something like:

from django.conf.urls import patterns, url

from ponies import views


urlpatterns = patterns('',
    url(r'^$', views.Index.as_view(), name='index'),
    url(r'^ponies/$', views.PonyList.as_view(), name='pony-list'),
    url(r'^ponies/create/$', views.PonyCreate.as_view(), name='pony-create'),
    url(r'^ponies/(?P<pk>\d+)/$', views.PonyDetail.as_view(), name='pony-detail'),
    url(r'^ponies/(?P<pk>\d+)/edit/$', views.PonyUpdate.as_view(), name='pony-update'),
)

The shortcuts you can use in patterns are really functional-view specific. The functional version looks much nicer:

from django.conf.urls import patterns, url


urlpatterns = patterns('ponies.views',
    url(r'^$', 'index', name='index'),
    url(r'^ponies/$', 'pony_list', name='pony-list'),
    url(r'^ponies/create/$', 'pony_create', name='pony-create'),
    url(r'^ponies/(?P<pk>\d+)/$', 'pony_detail', name='pony-detail'),
    url(r'^ponies/(?P<pk>\d+)/edit/$', 'pony_update, name='pony-update'),
)

So we can now have a class based view version which has the same feel:

from cbvpatterns import patterns, url


urlpatterns = patterns('ponies.views',
    url(r'^$', 'Index', name='index'),
    url(r'^ponies/$', 'PonyList', name='pony-list'),
    url(r'^ponies/create/$', 'PonyCreate', name='pony-create'),
    url(r'^ponies/(?P<pk>\d+)/$', 'PonyDetail', name='pony-detail'),
    url(r'^ponies/(?P<pk>\d+)/edit/$', 'PonyUpdate, name='pony-update'),
)

You can also pass in the actual view classes directly, rather than using the strnig representation.

Contributing

Development takes place on GitHub; pull requests are welcome. Run tests with tox.

django-cbvpatterns's People

Contributors

mjtamlyn avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.