Views

In Respite, views are encapsulated in classes according to the model they supervise. You are not required to subclass Respite’s Views class, but doing so will yield some things you might find useful:

class respite.Views

Base class for views.

Attribute template_path:
 A string describing a path to prefix templates with, or '' by default.
Attribute supported_formats:
 A list of strings describing formats supported by these views, or ['html'] by default.
_render(request, template=None, status=200, context={}, headers={}, prefix_template_path=True)

Render a HTTP response.

Parameters:
  • request – A django.http.HttpRequest instance.
  • template – A string describing the path to a template.
  • status – An integer describing the HTTP status code to respond with.
  • context – A dictionary describing variables to populate the template with.
  • headers – A dictionary describing HTTP headers.
  • prefix_template_path – A boolean describing whether to prefix the template with the view’s template path.

Please note that template must not specify an extension, as one will be appended according to the request format. For example, a value of blog/posts/index would populate blog/posts/index.html for requests that query the resource’s HTML representation.

If no template that matches the request format exists at the given location, or if template is None, Respite will attempt to serialize the template context automatically. You can change the way your models are serialized by defining serialize methods that return a dictionary:

class NuclearMissile(models.Model):
    serial_number = models.IntegerField()
    is_armed = models.BooleanField()
    launch_code = models.IntegerField()

    def serialize(self):
        return {
            'serial_number': self.serial_number,
            'is_armed': self.is_armed
        }

If the request format is not supported by the view (as determined by the supported_formats property or a specific view’s override_supported_formats decorator), this function will yield HTTP 406 Not Acceptable.

Default views

Respite defines a collection of views that facilitate for common features like viewing a list of items, viewing a specific item by its id, rendering a form to create a new item and so on. You can leverage these views by adding Respite’s Resource class to the base classes of your views class:

class PostViews(Views, Resource):
  model = Post
  template_path = 'posts/'
  supported_formats = ['html', 'json']
class respite.Resource

A collection of views that facilitate for common features.

Attribute model:
 A reference to a model.
Attribute form:A reference to a form, or None to generate one automatically.
index(request)

Render a list of objects.

show(request, id)

Render a single object.

new(request)

Render a form to create a new object.

create(request)

Create a new object.

update(request, id)

Update an object.

replace(request, id)

Replace an object.

destroy(request, id)

Delete an object.

Resource automatically generates routes for each of its views and names them appriopriately. In our example, the following routes would be generated:

HTTP path HTTP method View Name
posts/ GET index posts
posts/ POST create posts
posts/new GET new new_post
posts/1 GET show post
posts/1/edit GET edit edit_post
posts/1 PUT replace post
posts/1 PATCH update post
posts/1 DELETE destroy post

Table Of Contents

Previous topic

Routing

This Page