Skip to main content

No project description provided

Project description

Rails style controller dispatch method for Django class based views. Instead of dispatching incoming http request to HTTP verbs such as 'GET', 'POST', 'DELETE' etc, it dispatch to more familiar CRUD action such as create, update, delete, and delete. For browser based apps, I found the CRUD action make more sense, after all in the browser we only limited to GET and POST so not much gain to split our request handler into proper HTTP method.

Installation

pip install https://github.com/k4ml/django-viewrouter/archive/refs/heads/main.zip

Usage

Create a view class like below:-

from django.http import HttpResponse
from viewrouter.views import ActionView

class ArticleView(ActionView):
    def index(self, request):
        return HttpResponse('this is index')

    def retrieve(self, request, pk):
        return HttpResponse('this is article %s' % pk)

    def delete(self, request, pk):
        return HttpResponse('deleting %s' % pk)

    def update(self, request, pk):
        return HttpResponse('updating %s' % pk)

Then in your urls.py:-

from django.urls import include, path
from viewrouter.routers import Router

from yourproject.views import ArticleView

article_router = Router(ArticleView)
urlpatterns = patterns('',
    # Examples:
    # path('', 'myapp.views.home', name='home'),
    # path('blog/', include('blog.urls')),

    path('admin/', admin.site.urls),
    path('article/', article_router.urls)
)

The following url now accessible:-

  • /article/
  • /article/index/ (similar to above)
  • /article/update//
  • /article/retrieve//
  • /article/delete//

We can also use the following shortcuts instead:-

    path('article/', ArticleView.get_urls())

By default the following methods on your class based views will be automatically routed:-

  • index
  • retrieve
  • update
  • delete
  • create

Other methods can be routed by explicitly marking them with route decorator:-

from django.http import HttpResponse
from viewrouter.views import ActionView
from viewrouter.routers import route

class ArticleView(ActionView):
    def index(self):
        return HttpResponse('this is index')

    def retrieve(self, pk):
        return HttpResponse('this is article %s' % pk)

    @route(r'^set-password/(?<user_id>\d+)/')
    def set_password(self, user_id):
        return HttpResponse('change password for user %s' % user_id)

Unlike django built-in class based views which route based on HTTP methods, this will route all HTTP methods as long as the url pattern matched. To restrict the HTTP methods allowed, use http_methodsparameter to @route decorator:-

    @route(r'^set-password/(?<user_id>\d+)/', http_methods=['post'])
    def set_password(self, user_id):
        return HttpResponse('change password for user %s' % user_id)

Background

After start using class based views, I found our urls.py start getting cluttered with the boilerplate of wiring up the views into url patterns. And having split the views into proper http verbs, it mean we always need at least 2 separate views to handle the CRUD operation. For example:-

urlpatterns = patterns('',
        url(r'^article/$', ArticleView.as_view()),
        url(r'^article/(?P<pk>\d+)/$', ArticleEditorView.as_view()),
    )

Above, ArticleView will handle listing of articles and creating new one and ArticleEditorView will handle updating, displaying single article and deleting the article.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_viewrouter-0.1.3.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

django_viewrouter-0.1.3-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

Details for the file django_viewrouter-0.1.3.tar.gz.

File metadata

  • Download URL: django_viewrouter-0.1.3.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.10.12 Linux/6.2.0-1017-aws

File hashes

Hashes for django_viewrouter-0.1.3.tar.gz
Algorithm Hash digest
SHA256 af04f07ad392a135fc8e06fcc9758c501cffd623edefedc86470a9844337c0b7
MD5 fe0349b83ecf42a4cd14f6c6a2eba78d
BLAKE2b-256 706b50d834c00e211fba5e8ac1aa677d4480ea6b0b94b93f1b6ebf7377754fcb

See more details on using hashes here.

File details

Details for the file django_viewrouter-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: django_viewrouter-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 6.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.10.12 Linux/6.2.0-1017-aws

File hashes

Hashes for django_viewrouter-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 87fa94f5a84d681f2ce7a49abc24ffd7558fc0c24739b07e13ec31725db58185
MD5 2821c5d4a0769560c57d31eb7b9ce1cf
BLAKE2b-256 b0def4fd9407e8ff69acff033724dd2a61862a06fbb3fc7d194c72c0fc7f2eec

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page