django / django/new-features

Prevent purely numeric template variables

Open
#116 4 comments 12 reactions 0 assignees View on GitHub
Django Core Templates
Dominant language
No language data
Stars
188
Forks
7
PR merge metrics
No merged PRs in 30d

Description

### Code of Conduct

- [x] 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](https://blog.lilyf.org/posts/django-template-quirks-3/#invalid-numeric-literals). I also opened a [Trac ticket](https://code.djangoproject.com/ticket/36658), but was redirected here.

### Implementation Suggestions

_No response_

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.