CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-django-stubs

Comprehensive type stubs for Django framework enabling static type checking with mypy

Pending
Overview
Eval results
Files

templates.mddocs/

Template System

Django's template system provides secure HTML generation with automatic escaping, template inheritance, custom tags and filters, and multiple template engine support.

Capabilities

Template Engine

Core template engine classes for loading and rendering templates.

class Engine:
    """
    Template engine for loading and rendering templates.
    
    Main interface for template operations with configurable backends.
    """
    def __init__(self, dirs: list = None, app_dirs: bool = False, context_processors: list = None,
                 debug: bool = False, loaders: list = None, string_if_invalid: str = '',
                 file_charset: str = 'utf-8', libraries: dict = None, builtins: list = None,
                 autoescape: bool = True): ...
    
    def from_string(self, template_code: str) -> Template: ...
    def get_template(self, template_name: str) -> Template: ...
    def select_template(self, template_name_list: list) -> Template: ...
    def render_to_string(self, template_name: str, context: dict = None, request: HttpRequest = None) -> str: ...

engines: dict  # Template engines registry

Template Classes

Template objects for rendering content with context data.

class Template:
    """
    Compiled template ready for rendering.
    
    Contains parsed template nodes and rendering methods.
    """
    def __init__(self, template_string: str, origin=None, name: str = None, engine=None): ...
    
    def render(self, context: Context = None) -> str: ...
    def get_exception_info(self, exception: Exception, token) -> dict: ...
    
    name: str
    origin: Origin
    engine: Engine
    source: str
    nodelist: NodeList

class Origin:
    """
    Template origin information for debugging and caching.
    """
    def __init__(self, name: str, template_name: str = None, loader=None): ...
    
    name: str
    template_name: str
    loader: BaseLoader

Context System

Context classes for providing data to templates during rendering.

class Context(dict):
    """
    Template context for variable resolution.
    
    Stack-based context supporting nested scopes and context processors.
    """
    def __init__(self, dict_: dict = None, autoescape: bool = True, use_l10n: bool = None, use_tz: bool = None): ...
    
    def bind_template(self, template: Template) -> Context: ...
    def __enter__(self) -> Context: ...
    def __exit__(self, exc_type, exc_value, traceback) -> None: ...
    def push(self, other: dict = None) -> dict: ...
    def pop(self) -> dict: ...
    def __setitem__(self, key: str, value) -> None: ...
    def __getitem__(self, key: str): ...
    def __delitem__(self, key: str) -> None: ...
    def __contains__(self, key: str) -> bool: ...
    def get(self, key: str, otherwise=None): ...
    def setdefault(self, key: str, default=None): ...
    def new(self, values: dict = None) -> Context: ...
    def flatten(self) -> dict: ...
    
    template: Template
    render_context: RenderContext
    autoescape: bool
    use_l10n: bool
    use_tz: bool

class RequestContext(Context):
    """
    Context subclass that runs context processors automatically.
    
    Integrates request object and applies configured context processors.
    """
    def __init__(self, request: HttpRequest, dict_: dict = None, processors: list = None, 
                 use_l10n: bool = None, use_tz: bool = None, autoescape: bool = True): ...
    
    request: HttpRequest

class RenderContext(dict):
    """
    Stack-based context for template rendering state.
    """
    def __init__(self, dict_: dict = None): ...
    def __enter__(self) -> RenderContext: ...
    def __exit__(self, exc_type, exc_value, traceback) -> None: ...
    def push_state(self, engine: Engine, **kwargs) -> None: ...
    def pop_state(self) -> None: ...

Template Nodes

Node classes representing parsed template elements.

class Node:
    """
    Base class for template nodes.
    
    Represents a single element in the compiled template tree.
    """
    must_be_first: bool = False
    child_nodelists: tuple = ()
    
    def render(self, context: Context) -> str: ...
    def __iter__(self): ...
    def get_nodes_by_type(self, nodetype: type) -> list: ...

class NodeList(list):
    """
    List container for template nodes with rendering support.
    """
    def __init__(self, nodes: list = None): ...
    def render(self, context: Context) -> str: ...
    def get_nodes_by_type(self, nodetype: type) -> list: ...

class TextNode(Node):
    """
    Node for literal text content.
    """
    def __init__(self, s: str): ...
    
    s: str

class VariableNode(Node):
    """
    Node for template variable resolution and output.
    """
    def __init__(self, filter_expression: FilterExpression): ...
    
    filter_expression: FilterExpression

class CommentNode(Node):
    """
    Node for template comments (not rendered).
    """
    def __init__(self, comment_text: str = ''): ...
    
    comment_text: str

class IfNode(Node):
    """
    Node for conditional template logic.
    """
    def __init__(self, conditions_nodelists: list): ...
    
    conditions_nodelists: list

class ForNode(Node):
    """
    Node for template loops.
    """
    def __init__(self, loopvars: list, sequence: FilterExpression, is_reversed: bool, 
                 nodelist_loop: NodeList, nodelist_empty: NodeList = None): ...
    
    loopvars: list
    sequence: FilterExpression
    is_reversed: bool
    nodelist_loop: NodeList
    nodelist_empty: NodeList

class IncludeNode(Node):
    """
    Node for including other templates.
    """
    def __init__(self, template: FilterExpression, extra_context: dict = None, 
                 isolated_context: bool = False): ...

Variable Resolution

Classes for resolving template variables and applying filters.

class Variable:
    """
    Template variable with lookup and filtering support.
    
    Resolves dotted variable names in template context.
    """
    def __init__(self, var: str): ...
    
    def resolve(self, context: Context): ...
    def __repr__(self) -> str: ...
    def __str__(self) -> str: ...
    
    var: str
    literal: Any
    lookups: tuple
    translate: bool
    message_context: str

class FilterExpression:
    """
    Expression combining variable lookup with filters.
    
    Parses and applies template filters to variables.
    """
    def __init__(self, token: str, parser): ...
    
    def resolve(self, context: Context, ignore_failures: bool = False): ...
    def args_check(name: str, func, provided: list) -> bool: ...
    def __str__(self) -> str: ...
    
    token: str
    filters: list
    var: Variable

class Parser:
    """
    Template parser for processing template syntax.
    """
    def __init__(self, tokens: list): ...
    
    def parse(self, parse_until: list = None) -> NodeList: ...
    def skip_past(self, endtag: str) -> None: ...
    def extend_nodelist(self, nodelist: NodeList, node: Node, token) -> None: ...
    def error(self, token, msg: str) -> None: ...
    def invalid_block_tag(self, token, command: str, parse_until: list = None) -> None: ...
    def unclosed_block_tag(self, parse_until: list) -> None: ...
    def next_token(self): ...
    def prepend_token(self, token) -> None: ...
    def delete_first_token(self) -> None: ...
    
    tokens: list
    tags: dict
    filters: dict
    command_stack: list

Template Tags and Filters

System for registering custom template tags and filters.

class Library:
    """
    Registry for custom template tags and filters.
    
    Container for registering and organizing template extensions.
    """
    def __init__(self): ...
    
    def tag(self, name: str = None, compile_function=None): ...
    def tag_function(self, func): ...
    def filter(self, name: str = None, filter_func=None, expects_localtime: bool = False, 
               is_safe: bool = False, needs_autoescape: bool = False): ...
    def filter_function(self, func): ...
    def simple_tag(self, func=None, takes_context: bool = False, name: str = None): ...
    def inclusion_tag(self, filename: str, func=None, takes_context: bool = False, name: str = None): ...
    def assignment_tag(self, func=None, takes_context: bool = False, name: str = None): ...
    
    tags: dict
    filters: dict

def register_tag(library: str, tag: str): ...
def register_filter(library: str, filter_name: str): ...

Template Loaders

Classes for loading templates from various sources.

class BaseLoader:
    """
    Base template loader class.
    
    Abstract interface for loading templates from different sources.
    """
    def __init__(self, engine: Engine): ...
    
    def get_template(self, template_name: str, skip: list = None) -> Template: ...
    def get_template_sources(self, template_name: str): ...
    def load_template_source(self, template_name: str, template_dirs: list = None) -> tuple: ...
    
    engine: Engine

class FileSystemLoader(BaseLoader):
    """
    Load templates from filesystem directories.
    """
    def __init__(self, engine: Engine, dirs: list = None): ...

class AppDirectoriesLoader(BaseLoader):
    """
    Load templates from application directories.
    """
    def __init__(self, engine: Engine, dirs: list = None): ...

class CachedLoader(BaseLoader):
    """
    Wrapper loader that caches compiled templates.
    """
    def __init__(self, engine: Engine, loaders: list): ...

class LocmemLoader(BaseLoader):
    """
    Load templates from dictionary in local memory.
    """
    def __init__(self, engine: Engine, templates_dict: dict): ...

Template Response

Response classes that defer template rendering until response processing.

class TemplateResponse(HttpResponse):
    """
    HTTP response that renders template with context.
    
    Deferred rendering allows middleware to modify context before rendering.
    """
    def __init__(self, request: HttpRequest, template, context: dict = None, 
                 content_type: str = None, status: int = None, charset: str = None, using: str = None): ...
    
    def resolve_template(self, template) -> Template: ...
    def resolve_context(self, context: dict) -> Context: ...
    def render(self) -> TemplateResponse: ...
    def add_post_render_callback(self, callback) -> None: ...
    
    template_name: Any
    context_data: dict
    using: str
    is_rendered: bool
    post_render_callbacks: list

class SimpleTemplateResponse(HttpResponse):
    """
    Template response without request context processors.
    """
    def __init__(self, template, context: dict = None, content_type: str = None, 
                 status: int = None, charset: str = None, using: str = None): ...

Context Processors

Functions that add variables to template context automatically.

def debug(request: HttpRequest) -> dict:
    """
    Add debug and SQL query information to context.
    
    Args:
        request: HTTP request object
        
    Returns:
        Dictionary with debug and sql_queries keys
    """

def request(request: HttpRequest) -> dict:
    """
    Add request object to template context.
    
    Args:
        request: HTTP request object
        
    Returns:
        Dictionary with request key
    """

def csrf(request: HttpRequest) -> dict:
    """
    Add CSRF token to template context.
    
    Args:
        request: HTTP request object
        
    Returns:
        Dictionary with csrf_token key
    """

def static(request: HttpRequest) -> dict:
    """
    Add static files URL to template context.
    
    Args:
        request: HTTP request object
        
    Returns:
        Dictionary with STATIC_URL key
    """

def media(request: HttpRequest) -> dict:
    """
    Add media files URL to template context.
    
    Args:
        request: HTTP request object
        
    Returns:
        Dictionary with MEDIA_URL key
    """

def tz(request: HttpRequest) -> dict:
    """
    Add timezone information to template context.
    
    Args:
        request: HTTP request object
        
    Returns:
        Dictionary with TIME_ZONE key
    """

def i18n(request: HttpRequest) -> dict:
    """
    Add internationalization info to template context.
    
    Args:
        request: HTTP request object
        
    Returns:
        Dictionary with LANGUAGES and LANGUAGE_CODE keys
    """

Built-in Template Tags

Core template tags provided by Django.

# Control flow tags
def if_tag(parser: Parser, token) -> IfNode: ...
def for_tag(parser: Parser, token) -> ForNode: ...
def cycle_tag(parser: Parser, token) -> Node: ...
def firstof_tag(parser: Parser, token) -> Node: ...
def with_tag(parser: Parser, token) -> Node: ...

# Template inclusion tags  
def include_tag(parser: Parser, token) -> IncludeNode: ...
def extends_tag(parser: Parser, token) -> Node: ...
def block_tag(parser: Parser, token) -> Node: ...

# URL tags
def url_tag(parser: Parser, token) -> Node: ...

# Loading tags
def load_tag(parser: Parser, token) -> Node: ...

# Comment tags
def comment_tag(parser: Parser, token) -> CommentNode: ...

# Spacing tags
def spaceless_tag(parser: Parser, token) -> Node: ...
def autoescape_tag(parser: Parser, token) -> Node: ...

# Variable assignment tags
def regroup_tag(parser: Parser, token) -> Node: ...
def templatetag_tag(parser: Parser, token) -> Node: ...
def widthratio_tag(parser: Parser, token) -> Node: ...

# Conditional assignment tags
def ifchanged_tag(parser: Parser, token) -> Node: ...

# Debugging tags
def debug_tag(parser: Parser, token) -> Node: ...

# Localization tags
def localize_tag(parser: Parser, token) -> Node: ...
def get_current_language_tag(parser: Parser, token) -> Node: ...
def get_language_info_tag(parser: Parser, token) -> Node: ...
def get_language_info_list_tag(parser: Parser, token) -> Node: ...
def language_tag(parser: Parser, token) -> Node: ...
def trans_tag(parser: Parser, token) -> Node: ...
def blocktrans_tag(parser: Parser, token) -> Node: ...

# Static files tags
def static_tag(parser: Parser, token) -> Node: ...
def get_static_prefix_tag(parser: Parser, token) -> Node: ...
def get_media_prefix_tag(parser: Parser, token) -> Node: ...

Built-in Template Filters

Core template filters for data formatting and manipulation.

# String filters
def add(value, arg) -> str: ...
def addslashes(value) -> str: ...
def capfirst(value) -> str: ...
def center(value, width: int) -> str: ...
def cut(value, arg: str) -> str: ...
def escape(value) -> str: ...
def escapejs(value) -> str: ...
def floatformat(value, arg: int = -1) -> str: ...
def force_escape(value) -> str: ...
def linebreaks(value) -> str: ...
def linebreaksbr(value) -> str: ...
def linenumbers(value) -> str: ...
def ljust(value, width: int) -> str: ...
def lower(value) -> str: ...
def make_list(value) -> list: ...
def rjust(value, width: int) -> str: ...
def safe(value) -> str: ...
def safeseq(value) -> list: ...
def slugify(value) -> str: ...
def stringformat(value, format_str: str) -> str: ...
def striptags(value) -> str: ...
def title(value) -> str: ...
def truncatechars(value, length: int) -> str: ...
def truncatechars_html(value, length: int) -> str: ...
def truncatewords(value, length: int) -> str: ...
def truncatewords_html(value, length: int) -> str: ...
def upper(value) -> str: ...
def urlencode(value) -> str: ...
def urlize(value) -> str: ...
def urlizetrunc(value, length: int) -> str: ...
def wordcount(value) -> int: ...
def wordwrap(value, width: int) -> str: ...

# Number filters
def divisibleby(value, arg: int) -> bool: ...
def floatformat(value, arg: int = -1) -> str: ...

# Date filters  
def date(value, format_str: str = None) -> str: ...
def time(value, format_str: str = None) -> str: ...
def timesince(value, base_date=None) -> str: ...
def timeuntil(value, base_date=None) -> str: ...

# List filters
def dictsort(value, key: str) -> list: ...
def dictsortreversed(value, key: str) -> list: ...
def first(value): ...
def join(value, delimiter: str) -> str: ...
def last(value): ...
def length(value) -> int: ...
def length_is(value, length: int) -> bool: ...
def random(value): ...
def slice_filter(value, slice_str: str) -> list: ...
def unordered_list(value) -> str: ...

# Logic filters
def default(value, default_value): ...
def default_if_none(value, default_value): ...
def yesno(value, yes_no_maybe: str = None) -> str: ...

# File filters
def filesizeformat(value) -> str: ...

# Other filters
def pprint(value) -> str: ...
def phone2numeric(value) -> str: ...
def pluralize(value, suffix: str = 's') -> str: ...
def removetags(value, tags: str) -> str: ...
def iriencode(value) -> str: ...
def json_script(value, element_id: str) -> str: ...

Template Exceptions

Exception classes for template processing errors.

class TemplateDoesNotExist(Exception):
    """
    Template file not found error.
    
    Raised when requested template cannot be located by any loader.
    """
    def __init__(self, msg: str, tried: list = None, backend=None, chain: list = None): ...
    
    backend: Engine
    tried: list
    chain: list

class TemplateSyntaxError(Exception):
    """
    Template syntax parsing error.
    
    Raised when template contains invalid syntax or structure.
    """
    def __init__(self, msg: str, tried: list = None, backend=None, chain: list = None): ...

class VariableDoesNotExist(Exception):
    """
    Template variable resolution error.
    
    Raised when template variable cannot be resolved in context.
    """
    def __init__(self, msg: str, params: tuple = ()): ...
    
    msg: str
    params: tuple

Template Utilities

Helper functions for template operations and debugging.

def select_template(template_name_list: list, using: str = None) -> Template:
    """
    Load template from list, using first that exists.
    
    Args:
        template_name_list: List of template names to try
        using: Template engine name
        
    Returns:
        First existing template from list
    """

def get_template(template_name: str, using: str = None) -> Template:
    """
    Load and return template by name.
    
    Args:
        template_name: Name of template to load
        using: Template engine name
        
    Returns:
        Compiled template object
    """

def render_to_string(template_name: str, context: dict = None, request: HttpRequest = None, using: str = None) -> str:
    """
    Render template to string with given context.
    
    Args:
        template_name: Template name to render
        context: Template context data
        request: HTTP request for RequestContext
        using: Template engine name
        
    Returns:
        Rendered template as string
    """

def context_processors_list() -> list:
    """
    Get list of configured context processors.
    
    Returns:
        List of context processor functions
    """

Install with Tessl CLI

npx tessl i tessl/pypi-django-stubs

docs

admin.md

auth.md

contrib.md

database-orm.md

forms.md

http.md

index.md

migrations.md

mypy-plugin.md

signals.md

templates.md

transactions.md

urls.md

views.md

tile.json