Skip to content

Prevent purely numeric template variables #116

@LilyFirefly

Description

@LilyFirefly

Code of Conduct

  • I agree to follow Django's Code of Conduct

Feature Description

Raise a TemplateSyntaxError for template variables (and filter or template tag arguments) that start as a numeric literal. For example {{ 1.a }}, {{ 2.3.1 }}, {% if 12.3.c %}.

Problem

Django always treats a template variable like {{ 1 }} or {{ 2.3 }} as a numeric literal, so it is impossible to use a context with purely numeric keys to look up values. For example, the "foo" string in { 1: "foo"} or {"1": "foo"} can never be looked up. This means it's much more likely that an example like {{ 1.2.3 }} or {{ 1.a }} is a mistake by the template author than an intentional attempt to look up {1: {2: {3: 4}}} or {"1": {"a": "foo"}}.

I don't think we should prevent examples such as {{ 1a }}, since this can be looked up in a context such as {"1a": "foo"}.

Request or proposal

proposal

Additional Details

I talk about this in my blog post. I also opened a Trac ticket, but was redirected here.

Implementation Suggestions

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Django CoreThis idea is suitable for inclusion in Django itself.Templates

    Type

    No type

    Projects

    Status

    Idea

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions