Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8482301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:47:23+00:00 2026-06-10T19:47:23+00:00

In Python, if you have a Django form form with a field named foo

  • 0

In Python, if you have a Django form form with a field named foo, you can’t use the attribute access syntax form.foo to access the field; instead you must use form.fields['foo'].

But in a template you can — and normally do — use {{ form.foo }} to render the field, using the normal attribute access syntax that works for any object in templates.

Why/how does the template {{ form.foo }} work when Python form.foo doesn’t? Is it a bit of special handing that Django does when rendering templates? Or am I just misunderstanding something simple?

EDIT: I know that Django’s template language isn’t Python and there are many differences from Python. I am just curious why the {{ form.foo }} syntax works and/or how it’s implemented in the case of forms. After all, normally the template {{ object.attribute }} syntax DOES work analogously to the Python object.attribute syntax, but in the case of a form it seems to be handled as a special case.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T19:47:24+00:00Added an answer on June 10, 2026 at 7:47 pm

    Django’s templating language, as Kerrek SB mentions, isn’t Python, even if it sometimes looks like it. {{ object.attribute }} does appear to mimic Python’s attribute lookup, but it is just a special case of {{ a.b }} in the template language.

    When you use {{ a.b }} in a template, the interpreter will first look up a in the current context. Once it has that, it will try the following things to resolve a.b:

    • a["b"]
    • a.b
    • a.b() (in the case where b is a method of a)
    • a[b] (in the case where b is a number)

    For forms, specifically, a["b"] is what is used. You can refer to a field "foo" as form.fields["foo"] or as form["foo"]. That bit of magic comes from the form class, though, and not the template language.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Still learning django and python... I have a form with many fields. I want
I have Django 1.4 and Python 2.6.6 When I use django-amdin.py startproject djproject follow
Find the field names of inputtable form fields in a PDF document? I have
I have a Django application which includes a custom model field (and accompanying form
My problem is a python/django mix. I have a form model that will display
In Django/Python, when you make a custom form, does it need to have a
I have a Django app written in Python 2.5 and I plan to upgrade
I have a Python program (with Django - does this matter?) that I want
I intend to have many python with django applications on my server, is it
A python/django beginner's question: I have a datetime object (drive_date), a time object (start_time)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.