Inspired by Eric Florenzano's post about writing simple and very fast pure-WSGI applications. Using RestView approach. More about it on my blog
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | ### in urls.py
from django.conf.urls.defaults import *
from bench.songs.views import Song, All
urlpatterns = patterns('',
(r'^$', All()),
(r'^song/(?P<id>\d+)/', Song()),
)
### in songs/views.py
from django.http import HttpResponse
from collections import defaultdict
## RestView from http://www.djangosnippets.org/snippets/1071/
from bench.rest import RestView
counts = defaultdict(int)
class All(RestView):
def GET(self, request, *args):
res = ','.join(['%s=%s' % (k, v) for k, v in counts.iteritems()])
return HttpResponse(res, mimetype='text/plain')
def DELETE(self, request):
counts = defaultdict(int)
return HttpResponse('OK', mimetype='text/plain')
class Song(RestView):
def GET(self, request, id):
return HttpResponse(str(counts[id]), mimetype='text/plain')
def POST(self, request, id):
counts[id] += 1
return HttpResponse(str(counts[id]), mimetype='text/plain')
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 2 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
- Help text hyperlinks by sa2812 1 year, 7 months ago
Comments
Please login first before commenting.