Comprehensive type stubs for Django framework enabling static type checking with mypy
—
Django's view system provides flexible request handling through both function-based and class-based views, including generic views for common patterns like displaying lists, detail pages, and handling forms.
Core view classes providing the foundation for all Django views.
class View:
"""
Base class for all class-based views.
Provides dispatch method routing and basic HTTP method handling.
"""
http_method_names: list = ['get', 'post', 'put', 'patch', 'delete', 'head', 'options', 'trace']
def __init__(self, **kwargs): ...
@classmethod
def as_view(cls, **initkwargs): ...
def setup(self, request: HttpRequest, *args, **kwargs) -> None: ...
def dispatch(self, request: HttpRequest, *args, **kwargs) -> HttpResponse: ...
def http_method_not_allowed(self, request: HttpRequest, *args, **kwargs) -> HttpResponse: ...
def options(self, request: HttpRequest, *args, **kwargs) -> HttpResponse: ...
# HTTP method handlers
def get(self, request: HttpRequest, *args, **kwargs) -> HttpResponse: ...
def post(self, request: HttpRequest, *args, **kwargs) -> HttpResponse: ...
def put(self, request: HttpRequest, *args, **kwargs) -> HttpResponse: ...
def patch(self, request: HttpRequest, *args, **kwargs) -> HttpResponse: ...
def delete(self, request: HttpRequest, *args, **kwargs) -> HttpResponse: ...
def head(self, request: HttpRequest, *args, **kwargs) -> HttpResponse: ...
def trace(self, request: HttpRequest, *args, **kwargs) -> HttpResponse: ...
class TemplateView(TemplateResponseMixin, ContextMixin, View):
"""
Render a template with optional context data.
Generic view for displaying templates with context.
"""
def get(self, request: HttpRequest, *args, **kwargs) -> HttpResponse: ...
def get_context_data(self, **kwargs) -> dict: ...
class RedirectView(View):
"""
Redirect to a given URL or pattern name.
Supports permanent redirects and URL pattern resolution.
"""
permanent: bool = False
url: str = None
pattern_name: str = None
query_string: bool = False
def get_redirect_url(self, *args, **kwargs) -> str: ...
def get(self, request: HttpRequest, *args, **kwargs) -> HttpResponse: ...
def head(self, request: HttpRequest, *args, **kwargs) -> HttpResponse: ...
def post(self, request: HttpRequest, *args, **kwargs) -> HttpResponse: ...
def options(self, request: HttpRequest, *args, **kwargs) -> HttpResponse: ...
def delete(self, request: HttpRequest, *args, **kwargs) -> HttpResponse: ...
def put(self, request: HttpRequest, *args, **kwargs) -> HttpResponse: ...
def patch(self, request: HttpRequest, *args, **kwargs) -> HttpResponse: ...Views for displaying model data and object listings.
class ListView(MultipleObjectTemplateResponseMixin, BaseListView):
"""
Display a list of objects with pagination support.
Generic view for displaying querysets as paginated lists.
"""
model = None
queryset = None
paginate_by = None
paginate_orphans: int = 0
context_object_name: str = None
template_name: str = None
template_name_suffix: str = '_list'
allow_empty: bool = True
page_kwarg: str = 'page'
paginator_class = None
ordering = None
def get_queryset(self) -> QuerySet: ...
def get_context_data(self, **kwargs) -> dict: ...
def get_paginate_by(self, queryset: QuerySet) -> int: ...
def get_paginator(self, queryset: QuerySet, per_page: int, orphans: int = 0, allow_empty_first_page: bool = True): ...
def get_paginate_orphans(self) -> int: ...
def get_allow_empty(self) -> bool: ...
def get_context_object_name(self, object_list) -> str: ...
def paginate_queryset(self, queryset: QuerySet, page_size: int) -> tuple: ...
def get_ordering(self): ...
class DetailView(SingleObjectTemplateResponseMixin, BaseDetailView):
"""
Display a single object detail page.
Generic view for displaying individual model instances.
"""
model = None
queryset = None
slug_field: str = 'slug'
slug_url_kwarg: str = 'slug'
pk_url_kwarg: str = 'pk'
context_object_name: str = None
template_name: str = None
template_name_field: str = None
template_name_suffix: str = '_detail'
def get_object(self, queryset: QuerySet = None): ...
def get_queryset(self) -> QuerySet: ...
def get_slug_field(self) -> str: ...
def get_context_data(self, **kwargs) -> dict: ...
def get_context_object_name(self, obj) -> str: ...Views for handling form display and processing.
class FormView(TemplateResponseMixin, BaseFormView):
"""
Display a form and handle form processing.
Generic view for displaying and processing forms.
"""
form_class = None
success_url: str = None
template_name: str = None
def get_form_class(self): ...
def get_form(self, form_class=None): ...
def get_form_kwargs(self) -> dict: ...
def get_initial(self) -> dict: ...
def form_valid(self, form) -> HttpResponse: ...
def form_invalid(self, form) -> HttpResponse: ...
def get_context_data(self, **kwargs) -> dict: ...
def get_success_url(self) -> str: ...
class ProcessFormView(View):
"""
Base view for processing forms with GET and POST handling.
"""
def get(self, request: HttpRequest, *args, **kwargs) -> HttpResponse: ...
def post(self, request: HttpRequest, *args, **kwargs) -> HttpResponse: ...
def put(self, *args, **kwargs) -> HttpResponse: ...Views for creating, updating, and deleting model instances.
class CreateView(SingleObjectTemplateResponseMixin, BaseCreateView):
"""
Create a new model instance with a form.
Generic view for creating new objects through forms.
"""
model = None
form_class = None
fields = None
success_url: str = None
template_name: str = None
template_name_suffix: str = '_form'
def get_object(self, queryset: QuerySet = None): ...
def form_valid(self, form) -> HttpResponse: ...
def get_form_class(self): ...
def get_form_kwargs(self) -> dict: ...
def get_success_url(self) -> str: ...
class UpdateView(SingleObjectTemplateResponseMixin, BaseUpdateView):
"""
Update an existing model instance with a form.
Generic view for editing existing objects through forms.
"""
model = None
form_class = None
fields = None
slug_field: str = 'slug'
slug_url_kwarg: str = 'slug'
pk_url_kwarg: str = 'pk'
success_url: str = None
template_name: str = None
template_name_suffix: str = '_form'
def get_object(self, queryset: QuerySet = None): ...
def form_valid(self, form) -> HttpResponse: ...
def get_form_class(self): ...
def get_form_kwargs(self) -> dict: ...
def get_success_url(self) -> str: ...
class DeleteView(SingleObjectTemplateResponseMixin, BaseDeleteView):
"""
Delete an existing model instance with confirmation.
Generic view for deleting objects with confirmation page.
"""
model = None
queryset = None
slug_field: str = 'slug'
slug_url_kwarg: str = 'slug'
pk_url_kwarg: str = 'pk'
success_url: str = None
template_name: str = None
template_name_suffix: str = '_confirm_delete'
def get_object(self, queryset: QuerySet = None): ...
def delete(self, request: HttpRequest, *args, **kwargs) -> HttpResponse: ...
def post(self, request: HttpRequest, *args, **kwargs) -> HttpResponse: ...
def get_success_url(self) -> str: ...Views for displaying objects filtered by date and time.
class ArchiveIndexView(MultipleObjectTemplateResponseMixin, BaseArchiveIndexView):
"""
Display archive index of date-based objects.
Top-level index page showing latest objects by date.
"""
date_field: str = None
allow_future: bool = False
allow_empty: bool = True
template_name_suffix: str = '_archive'
def get_dated_items(self) -> tuple: ...
def get_dated_queryset(self, **lookup) -> QuerySet: ...
def get_date_field(self) -> str: ...
def get_allow_future(self) -> bool: ...
class YearArchiveView(MultipleObjectTemplateResponseMixin, BaseYearArchiveView):
"""
Display objects for a specific year.
Archive view filtered by year with optional month navigation.
"""
year_format: str = '%Y'
date_field: str = None
make_object_list: bool = False
allow_future: bool = False
allow_empty: bool = False
template_name_suffix: str = '_archive_year'
def get_year_format(self) -> str: ...
def get_year(self) -> str: ...
def get_next_year(self, date): ...
def get_previous_year(self, date): ...
def get_dated_items(self) -> tuple: ...
def get_make_object_list(self) -> bool: ...
class MonthArchiveView(MultipleObjectTemplateResponseMixin, BaseMonthArchiveView):
"""
Display objects for a specific month.
Archive view filtered by year and month.
"""
year_format: str = '%Y'
month_format: str = '%b'
date_field: str = None
allow_future: bool = False
allow_empty: bool = False
template_name_suffix: str = '_archive_month'
def get_month_format(self) -> str: ...
def get_month(self) -> str: ...
def get_next_month(self, date): ...
def get_previous_month(self, date): ...
def get_dated_items(self) -> tuple: ...
class WeekArchiveView(MultipleObjectTemplateResponseMixin, BaseWeekArchiveView):
"""
Display objects for a specific week.
Archive view filtered by year and week.
"""
year_format: str = '%Y'
week_format: str = '%U'
date_field: str = None
allow_future: bool = False
allow_empty: bool = False
template_name_suffix: str = '_archive_week'
def get_week_format(self) -> str: ...
def get_week(self) -> str: ...
def get_next_week(self, date): ...
def get_previous_week(self, date): ...
def get_dated_items(self) -> tuple: ...
class DayArchiveView(MultipleObjectTemplateResponseMixin, BaseDayArchiveView):
"""
Display objects for a specific day.
Archive view filtered by year, month, and day.
"""
year_format: str = '%Y'
month_format: str = '%b'
day_format: str = '%d'
date_field: str = None
allow_future: bool = False
allow_empty: bool = False
template_name_suffix: str = '_archive_day'
def get_day_format(self) -> str: ...
def get_day(self) -> str: ...
def get_next_day(self, date): ...
def get_previous_day(self, date): ...
def get_dated_items(self) -> tuple: ...
class TodayArchiveView(MultipleObjectTemplateResponseMixin, BaseTodayArchiveView):
"""
Display objects for today's date.
Archive view filtered to current date.
"""
template_name_suffix: str = '_archive_today'
class DateDetailView(SingleObjectTemplateResponseMixin, BaseDateDetailView):
"""
Display a single object on a specific date.
Detail view that includes date-based filtering.
"""
year_format: str = '%Y'
month_format: str = '%b'
day_format: str = '%d'
date_field: str = None
allow_future: bool = False
template_name_suffix: str = '_detail'
def get_object(self, queryset: QuerySet = None): ...Mixins providing reusable functionality for views.
class ContextMixin:
"""
Mixin for adding extra context to template views.
"""
extra_context: dict = None
def get_context_data(self, **kwargs) -> dict: ...
class TemplateResponseMixin:
"""
Mixin for views that render templates.
Provides template selection and response rendering.
"""
template_name: str = None
template_engine: str = None
response_class = None
content_type: str = None
def render_to_response(self, context: dict, **response_kwargs) -> HttpResponse: ...
def get_template_names(self) -> list: ...
class SingleObjectMixin(ContextMixin):
"""
Mixin for views that work with single model instances.
"""
model = None
queryset = None
slug_field: str = 'slug'
context_object_name: str = None
slug_url_kwarg: str = 'slug'
pk_url_kwarg: str = 'pk'
query_pk_and_slug: bool = False
def get_object(self, queryset: QuerySet = None): ...
def get_queryset(self) -> QuerySet: ...
def get_slug_field(self) -> str: ...
def get_context_object_name(self, obj) -> str: ...
def get_context_data(self, **kwargs) -> dict: ...
class MultipleObjectMixin(ContextMixin):
"""
Mixin for views that work with multiple objects.
"""
allow_empty: bool = True
queryset = None
model = None
paginate_by = None
paginate_orphans: int = 0
context_object_name: str = None
paginator_class = None
page_kwarg: str = 'page'
ordering = None
def get_queryset(self) -> QuerySet: ...
def get_ordering(self): ...
def paginate_queryset(self, queryset: QuerySet, page_size: int) -> tuple: ...
def get_paginate_by(self, queryset: QuerySet) -> int: ...
def get_paginator(self, queryset: QuerySet, per_page: int, orphans: int = 0, allow_empty_first_page: bool = True): ...
def get_paginate_orphans(self) -> int: ...
def get_allow_empty(self) -> bool: ...
def get_context_object_name(self, object_list) -> str: ...
def get_context_data(self, **kwargs) -> dict: ...
class FormMixin(ContextMixin):
"""
Mixin for views that handle forms.
"""
initial: dict = {}
form_class = None
success_url: str = None
prefix: str = None
def get_initial(self) -> dict: ...
def get_prefix(self) -> str: ...
def get_form_class(self): ...
def get_form(self, form_class=None): ...
def get_form_kwargs(self) -> dict: ...
def get_success_url(self) -> str: ...
def form_valid(self, form) -> HttpResponse: ...
def form_invalid(self, form) -> HttpResponse: ...
def get_context_data(self, **kwargs) -> dict: ...
class ModelFormMixin(FormMixin, SingleObjectMixin):
"""
Mixin for views that handle model forms.
"""
fields = None
def get_form_class(self): ...
def get_form_kwargs(self) -> dict: ...
def get_success_url(self) -> str: ...
def form_valid(self, form) -> HttpResponse: ...
class ProcessFormView(View):
"""
Base view for processing forms.
"""
def get(self, request: HttpRequest, *args, **kwargs) -> HttpResponse: ...
def post(self, request: HttpRequest, *args, **kwargs) -> HttpResponse: ...Decorators for enhancing function-based views.
def require_http_methods(request_method_list: list):
"""
Decorator to require specific HTTP methods.
Args:
request_method_list: List of allowed HTTP methods
"""
def require_GET(view_func):
"""Decorator to require GET method."""
def require_POST(view_func):
"""Decorator to require POST method."""
def require_safe(view_func):
"""Decorator to require safe methods (GET, HEAD)."""
def condition(etag_func=None, last_modified_func=None):
"""
Decorator for conditional view processing.
Args:
etag_func: Function to calculate ETag
last_modified_func: Function to calculate last modified date
"""
def etag(etag_func):
"""
Decorator to add ETag headers.
Args:
etag_func: Function to calculate ETag value
"""
def last_modified(last_modified_func):
"""
Decorator to add Last-Modified headers.
Args:
last_modified_func: Function to calculate last modified date
"""
def vary_on_cookie(view_func):
"""Decorator to add Vary: Cookie header."""
def vary_on_headers(*headers):
"""
Decorator to add Vary headers.
Args:
headers: Header names to vary on
"""
def cache_control(**kwargs):
"""
Decorator to add Cache-Control headers.
Args:
**kwargs: Cache control directives
"""
def never_cache(view_func):
"""Decorator to prevent caching."""
def cache_page(timeout: int, cache: str = None, key_prefix: str = None):
"""
Decorator for page-level caching.
Args:
timeout: Cache timeout in seconds
cache: Cache backend name
key_prefix: Cache key prefix
"""Utilities for creating specialized view responses.
def render(request: HttpRequest, template_name: str, context: dict = None,
content_type: str = None, status: int = None, using: str = None) -> HttpResponse:
"""
Render template with context to HttpResponse.
Args:
request: HTTP request object
template_name: Template name to render
context: Template context dictionary
content_type: Response content type
status: HTTP status code
using: Template engine name
"""
def render_to_response(template_name: str, context: dict = None,
content_type: str = None, status: int = None, using: str = None) -> HttpResponse:
"""
Render template to HttpResponse without request context.
Args:
template_name: Template name to render
context: Template context dictionary
content_type: Response content type
status: HTTP status code
using: Template engine name
"""
def redirect(to, permanent: bool = False, *args, **kwargs) -> HttpResponse:
"""
Return redirect response to given URL or view.
Args:
to: URL, view name, or model instance to redirect to
permanent: Whether to use permanent redirect (301 vs 302)
*args: Arguments for URL resolution
**kwargs: Keyword arguments for URL resolution
"""
def get_object_or_404(klass, *args, **kwargs):
"""
Get object or raise Http404 if not found.
Args:
klass: Model class or Manager
*args: Lookup arguments
**kwargs: Lookup keyword arguments
"""
def get_list_or_404(klass, *args, **kwargs):
"""
Get object list or raise Http404 if empty.
Args:
klass: Model class or Manager
*args: Filter arguments
**kwargs: Filter keyword arguments
"""Install with Tessl CLI
npx tessl i tessl/pypi-django-stubs