Opened 10 years ago

Closed 10 years ago

Last modified 10 years ago

#23114 closed Bug (fixed)

Template Variables Docs Incorrect/Unclear

Reported by: tom.dalton@… Owned by: Gabriel Muñumel
Component: Documentation Version: 1.6
Severity: Normal Keywords: template
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

On this section https://docs.djangoproject.com/en/1.6/topics/templates/#variables
In the "Behind the Scenes" section, it gives the order that Django tries to resolve a variable like "X.Y".
The 4 things that it tries are accurate, but the way/order they're done doesn't appear to be correct.

Ref the code that does this:
https://github.com/django/django/blob/master/django/template/base.py#L756

What actually (seems to) happen is:

  • Try dictionary lookup to get "thing"
  • If not found "thing", try attribute lookup to get "thing"
  • If not found "thing", try list-index lookup to get "thing"
  • If not found "thing", then fail
  • If "thing" is callable, then "thing" = "thing()"
  • Return "thing"

Applies in version 1.6 and onwards, probably applicable to older doc versions too.

Change History (10)

comment:1 by Baptiste Mispelon, 10 years ago

Easy pickings: set
Triage Stage: UnreviewedAccepted

Hi,

It seems this list has remained largely unchanged since its initial introduction (096ad32c845f850278b942d087e607dc4311b5f9).

I don't see a reason to distinguish argument lookups and method calls. I think we could simply merge the two items into one: "Attribute/method lookup".

THanks

comment:2 by Gabriel Muñumel, 10 years ago

Owner: changed from nobody to Gabriel Muñumel
Status: newassigned

comment:3 by anonymous, 10 years ago

Even that's not quite accurate, since it tries the initial lookup (dict then attribute then list index). Once it's got that value, it then calls it if possible.

So maybe the docs should say something like:

Value lookup is done in the following order:

  • dict-key
  • attribute
  • list-index

If the value is callable, it is called, and the value becomes the result of that call.

comment:4 by tom.dalton@…, 10 years ago

So I guess I'm saying there are 2 issues:

  1. The Attribute lookup vs Method call thing.
  2. It's not stated that other values (e.g. if dictionary or list lookup worked) are actually called. The docs imply only methods are called.

comment:5 by Baptiste Mispelon, 10 years ago

Yes, good point.

comment:6 by Gabriel Muñumel, 10 years ago

comment:7 by anonymous, 10 years ago

Thanks for that. I added a comment to the pull request with a patch for a slightly more comprehensive rework to include the stuff talked about enough. I don't think that just combining attribute/method lookup is enough to clarify what Django actually does behind the scenes.

comment:8 by Baptiste Mispelon <bmispelon@…>, 10 years ago

Resolution: fixed
Status: assignedclosed

In 62c74abbb06a081708c2cb5cc459f39beb26acaa:

Fixed #23114 -- Clarified documentation of template lookups.

Thanks to gmunumel and Tom Dalton for their help on the patch.

comment:9 by Baptiste Mispelon <bmispelon@…>, 10 years ago

In 13d44f54a265e9fc6185e1d5518da8ad568fbaad:

[1.7.x] Fixed #23114 -- Clarified documentation of template lookups.

Thanks to gmunumel and Tom Dalton for their help on the patch.

Backport of 62c74abbb06a081708c2cb5cc459f39beb26acaa from master.

comment:10 by anonymous, 10 years ago

Thanks for the speedy resolution!

Note: See TracTickets for help on using tickets.
Back to Top