12.3.1 Views

A view is any Python callable that takes an HttpRequest instance and returns an HttpResponse object.

Typically, views are either (1) a function or (2) an object instantiated by a subclass of View. Developers could build their own class for providing the basic behavior of a view, but doing so turns out to be tricky. To best avoid security problems, developers typically stick to using View when building class-based views (CBVs) because it is safer and quicker.

The advantage of using a function for a view is the simplicity of a function. The advantage of using a CBV is better adherence to HTTP. Similar behavior is available to developers when programming function views thanks to the require_http_methods decorator (and its siblings, ...

Get Django Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.