Respite

Conforms Django to REST
Download

Respite Ranking & Summary

Advertisement

  • Rating:
  • License:
  • MIT/X Consortium Lic...
  • Publisher Name:
  • Johannes Gorset
  • Publisher web site:
  • http://github.com/jgorset/

Respite Tags


Respite Description

Conforms Django to REST Respite is an app that conforms Django to Representational State Transfer (REST).UsagePrimerRespite is influenced by Ruby on Rails, though in the spirit of Python it is not nearly as "magic". It will, however, save you a lot of code:# news/models.pyfrom django.db import modelsclass Article(models.Model): title = models.CharField(max_length=255) content = models.TextField() published = True created_at = models.DateTimeField(auto_now_add=True)# news/urls.pyfrom django.conf.urls.defaults import *from respite.urls import resourcefrom views import ArticleViewurlpatterns = resource( prefix = 'news/articles', view = ArticleView)# news/views.pyfrom respite import Viewfrom models import Articleclass ArticleView(View): model = Article template_path = 'news/articles' supported_formats = # templates/news/articles/index.html< !DOCTYPE html >< html > < head > < title >{{ article.title }}< /title > < /head > < body > {% for article in articles %} < article > < h1 >< a href="{% url news_article id=article.id %}" >{{ article.title }}< /a >< /h1 > < time datetime="{{ article.created_at.isoformat }}" >{{ article.created_at }} < p > {{ article.content }} < /p > < /article > {% endfor %} < /body >< /html ># templates/news/articles/index.json# ...Default actionsRespite's View class defines actions for viewing and manipulating model instances; index, show, new, create, edit‚ update and destroy.HTTP method HTTP path Function PurposeGET articles/ index Render a list of articlesGET articles/new new Render a form to create a new articlePOST articles/ create Create a new articleGET articles/1 show Render a specific articleGET articles/1/edit edit Render a form to edit a specific articlePUT articles/1 update Edit a specific articleDELETE articles/1 destroy Delete a specific articleIn a nutshell, Respite provides you with a collection of features you probably need for most of your models and routes them RESTfully. You may override any or all of these functions and customize them as you'd like. For example, you could only list articles that have been published:# news/views.pyclass ArticleView(View): model = Article template_path = 'news/articles' supported_formats = def index(self, request): articles = self.model.objects.filter(published=True) return self._render( request = request, template = 'index', context = { 'articles': articles, }, status = 200 )You may also omit one or several of the default actions altogether. For example, you could only implement the index and show actions:# news/urls.pyfrom django.conf.urls.defaults import *from respite.urls import resourcefrom views import ArticleViewurlpatterns = resource( prefix = 'news/articles', view = ArticleView, actions = )Custom actionsYou are not limited to Respite's seven predefined actions; you may add any number of custom actions and route them however you like:# news/urls.pyfrom django.conf.urls.defaults import *from respite.urls import resource, actionfrom views import ArticleViewurlpatterns = resource( prefix = 'news/articles', view = ArticleView, custom_actions = +)/preview\.?*$', function = 'preview', methods = , name = 'preview_news_article' ) ])# news/views.pyfrom respite import Viewfrom models import Articleclass ArticleView(View): model = Article template_path = 'news/articles' supported_formats = def preview(self, request, id): article = Article.objects.get(id=id) return self._render( request = request, template = 'preview', context = { 'article': article }, status = 200 )Installation- pip install git+http://github.com/jgorset/respite.git- Add respite to INSTALLED_APPS in your settings file- Add respite.middleware.HTTPPUTMiddleware to MIDDLEWARE_CLASSES in your settings fileIf you're not just building an API, you might also want to add respite.middleware.HTTPMethodOverrideMiddleware to your middleware classes; it facilitates for overriding the HTTP method with the X-HTTP-Method-Override header or a _method HTTP POST parameter, which is the only way to update (HTTP PUT) and delete (HTTP DELETE) resources from a web browser. Requirements: · Python · Django


Respite Related Software