With Django Template: Keep the Name Inside Its Block

A local count disappears after endwith, and a fallback added everywhere also hides a real zero. Scope and false-value handling are separate questions, so inspect both with a small context.

The browser companion generates a context, a Django template, and expected before/inside/after values using a Django 5.2 reference example. It lets you compare a local binding with the caller’s values. It does not execute Django, so its expected results still need a render in your own environment.

With Django Template: Keep the Name Inside Its Block
With Django Template: Keep the Name Inside Its Block
Open the full example ↗

Use the worked example

Set attendees to 0 and choose Inspect. Inside the block, the expected text is Practice:0; afterward, the outer label is Outside and count is UNBOUND. You can also enter a title containing markup and inspect the escaping expectation alongside the generated source.

Follow the source and its scope

Django’s 5.2 documentation for with describes local bindings inside a block, including multiple bindings. In this example, the generated context has a session object with title Practice and attendees 3. A separate root label is Outside, and there is no root count. Those inputs let you see both shadowing an existing name and introducing a new one.

The generated template opens with {% with count=session.attendees label=session.title %}. Its inside output is {{ label }}:{{ count }}, followed by {% endwith %}. After the block it outputs {{ label }}:{{ count|default:"UNBOUND" }}. Read the source and expected rows together: the row heading “Inside” identifies the scope; it is not part of the template’s output.

Before entering the block, label refers to the caller’s Outside value. Inside, the local label refers to Practice and count refers to 3. Leaving the block exposes Outside again and removes the local count. The template has not permanently rewritten the caller’s label or added count to the root context.

Both bindings read from the session object. They do not depend on the value of another name newly introduced in the same opening tag. Keep that relationship clear when adapting the source: this example needs two lookups, not sequential Python assignments or arithmetic in the tag.

Change only attendees to 0 to separate scope from false-value handling. Direct {{ count }} inside the block should still produce 0. Its local binding exists even though the supplied value is zero; the later UNBOUND marker refers to a different situation, the absent root count.

The companion accepts a nonnegative whole attendee count. After changing a field, choose Inspect to regenerate the comparison; the old result is cleared when you edit. Reset restores Practice and 3. These controls help you prepare cases for a Django render without mistaking a stale expected row for a result from the new context.

UNBOUND is the fallback string written into this example’s post-block expression. It is neither an attendee count nor a Python exception. If your application uses different missing-variable settings or fallback text, include that configuration when comparing an actual render with these expectations.

For an escaping case, set the title to the ordinary string <em>Practice</em> and restore attendees to 3. The expected serialized inside text is &lt;em&gt;Practice&lt;/em&gt;:3. With automatic escaping enabled, a browser should display the angle-bracketed text instead of turning the supplied title into an emphasized element.

Django’s default filter uses its fallback for false-valued input. Putting that filter on the inside count would therefore obscure the legitimate zero. The source deliberately uses direct output inside and a fallback after the block, where count is absent in the supplied context.

Keep automatic escaping enabled for the markup-title comparison. Applying safe or changing the autoescape setting would create a different case with different output; the browser companion does not offer those settings.

The markup title changes the expected inside text, while the outer label stays Outside. Compare those two scopes separately before using the source in a larger template.

Copy a prompt for this example

Create one HTML source companion explaining Django's with/endwith scope using the Django 5.2 documentation as its reference version. This browser page generates context and template text plus expected values; it must not imply that JavaScript executes a Django template engine.

Start with context session.title = Practice, session.attendees = 3, and root label = Outside. There is no root count. Generate a template binding count=session.attendees and label=session.title inside a with block. Inside, output label and count directly. After endwith, output root label and count with default:"UNBOUND" for the absent count.

Provide editable title and nonnegative integer attendee fields, Inspect expected scope, and Reset. The expected inside row is Practice:3; after the block label is Outside and count is UNBOUND. Changing attendees to zero must preserve Practice:0 inside. Explain why applying default to the inside count would hide that legitimate zero.

If title is an ordinary string containing markup, show the expected escaped serialization as text. Keep expected escaping separate from an actual engine result. Display the complete generated JSON context, the template snippet, and a before/inside/after table with an explicit Actual engine result: Not executed row.

Editing either field clears old scope feedback until Inspect is used again. Invalid attendee input gets a correction message. Reset restores Practice and three attendees. Use wrapped code panels and readable stacked sections on small screens. Return one complete HTML file.

Run the zero and markup-title cases in your Django environment next. The prepared HTML explanation can be previewed in NOS for the code discussion. Keep its expected rows labeled as expectations until you have compared them with the real template engine’s output.

Keep reading