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:
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 a HTTP response.
Parameters: |
|
---|
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.
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']
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. |
Render a list of objects.
Render a single object.
Render a form to create a new object.
Create a new object.
Update an object.
Replace an object.
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