Code Reference

View & Shortcut Methods

Django · Methods reference

Django · Methods reference


📋 Overview

Function/ class views and common django.shortcuts.

🔧 Methods

Shortcuts

FunctionDescription
render(request, template, context)HttpResponse with template
redirect(to, …)302/301 redirect response
get_object_or_404(Model, …)get() or Http404
get_list_or_404(qs, …)Non-empty list or Http404

Class-based views

Method / attributeDescription
as_view()Class → callable for URLconf
dispatch(request, …)Route HTTP method to handler
get / post / put / deleteHTTP verb handlers
get_context_data(**kwargs)Build template context
form_valid / form_invalidFormView hooks

Decorators

DecoratorDescription
@login_requiredRedirect unauthenticated users
@permission_required(codename)403 if missing perm
@require_http_methods([…])Allow only listed verbs
@csrf_exemptDisable CSRF check (use carefully)

💡 Examples

See parent topic notes for runnable snippets; this page is the complete method index.

⚠️ Pitfalls

  • Mutating methods return None in Python — do not chain sort() / reverse() expecting a new list.
  • Default JS sort() compares strings — pass (a,b) => a-b for numbers.
  • SQL function names differ by dialect — verify Postgres vs MySQL docs.
  • Django QuerySet.update() skips save() signals and auto auto_now fields on models.

On this page