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.
from django.db import models
class BlogPost(models.Model):
slug = models.SlugField()
title = models.CharField()
text = models.TextField()
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')
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
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.