Coder Social home page Coder Social logo

django-viewsets's Introduction

django-viewsets

A different way of constructing Django class-based views

Django's class based views provide many advantages over 'traditional' function based views. They simplify the handling of different HTTP methods and do much of the work in setting up and handling forms.

But the downside is that each class basically handles only one endpoint. You then have to stitch them together in the urls file. And the names of the endpoints there kind of look like the names of your classes. It almost seems like... repeating yourself.

Now, Django REST Framework has the concept of a ViewSet. Firstly they handle the different HTTP methods - handling POST is via a create method, for example. But they also allow extra views to be annotated as either a 'list' or a 'detail' view, so for instance you can have a /author/ list view of all authors, an /author/sales/ list view of the sales information for all authors, an /author/Shakespeare/ detail view of Shakespeare, and an /author/Shakespeare/books/ detail view for Shakespeare's publications. DRF's 'routers' can then introspect the ViewSet to find which URLs it publishes and maps them automatically.

django-viewsets provides the best of both worlds for Django users.

Example:

models.py

from django.db import models

class BlogPost(models.Model):
    slug = models.SlugField()
    title = models.CharField()
    text = models.TextField()

views.py

from my_site.models import BlogPost
from viewsets.decorators import detail
from viewsets import ViewSet, ModelViewSet

class HomePage(ViewSet):
    """
    A simple handler for the home page index.
    """
    template_dir = 'home'

    def index(self, request):
        return self.render()

class BlogViewSet(ModelViewSet):
    lookup = 'slug'
    queryset = BlogPost.objects.all()
    template_dir = 'home/blog'

    # list, detail, create, destroy already included

    @detail()
    def stats(self, request, slug):
        post = self.get_object(slug=slug)
        # render assumes template = action name otherwise:
        return self.render(post, template='blog_stats')

urls.py

from django.conf.urls import url, include
from viewsets.routers import DefaultRouter
from views import HomePage, BlogViewSet

router = DefaultRouter()
router.add('^', HomePage, 'home')
router.add('blog', BlogViewSet)

urlpatterns = [url(r'^', include(router.urls)),]

django-viewsets's People

Contributors

paulway avatar

Stargazers

 avatar

Watchers

 avatar  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.