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 8987701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:51:28+00:00 2026-06-15T21:51:28+00:00

I have a Python object that is an instance of a subclass of container.

  • 0

I have a Python object that is an instance of a subclass of container. So this object, x, can behave like x['name']. This object also has methods, say x.bar().

How do I call x.bar from a Django template? In the template x.bar will always be evaluated as x['bar'] first, which gives None.

The best I can think of is to create an object shadowing x, that does not have getitem. But I can’t delattr __getitem__.

Any better ideas? Short of passing in results of those calls manually in context or a template tag.

Thanks.

  • 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-15T21:51:29+00:00Added an answer on June 15, 2026 at 9:51 pm

    This is a fundamental flaw in the way Django templates do expression evaluation. Look at the template tag {% expr -arbitrary-python-expression- %} for a workaround. E.g. {% expr x.bar(1,2,3) as snort %} gives you a new variable in the current Context named snort.

    Alternatively, you could try Jinja2 templates. They are very close to Django’s, but they allow full Python.

    Note: Jinja2 is not the same templating system as Django’s, and if you are using any page-embedded django app’s that depend on the templating system, this may break them. That is not true for the {% expr %} tag.

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

Sidebar

Related Questions

It seems that Python has some limitations regarding instance methods. Instance methods can't be
I have a Python datetime object that I want to convert to unix time,
I want a Python object that will monitor whether other objects have changed since
I have an object that is basically a Python implementation of an Oracle sequence.
I have a Python application in the bottle web-server that accesses a C shared-object
I have a bytearray object in Python 2.7 that I got from a query
I have the following simple methods for writing a python object to a file
I have a function that looks like this: def getCurrentShow(hour=(localtime().tm_hour),day=datetime.datetime.now().strftime('%A')): return Schedule.objects.get(hour=hour,day=day).show so I
I have some python module, which has a class ModuleClass and I can't modify
The python documentation for __del__ explains that an object's __del__ method can be called

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.