Code

Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#15660 closed (invalid)

"if" tag doesn't recognize "class_obj in instance.list_of_classes"

Reported by: donspaulding Owned by: nobody
Component: Template system Version: master
Severity: Keywords:
Cc: donspaulding Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

I was writing a template today, and couldn't figure out where my logic was broken. After a good bit of researching the new "smart if" tag, reading through the smartif.py module, and testing, I believe the if tag is broken when evaluating whether a context variable is contained within a list on another context variable.

I had a list of classes as a class attribute of an object, and wanted to see if a different class object was in that list. The following classes are a stripped down version of my context variables.

class PhoneSupport(Service):
    pass

class EmailSupport(Service):
    pass

class LiveSupport(Service):
    pass

class Basic(Bundle):
    services = [PhoneSupport, EmailSupport]

class Premium(Bundle):
    services = [PhoneSupport, EmailSupport, LiveSupport]

class Site(object):
    services = [PhoneSupport, EmailSupport, LiveSupport]
    bundles = [Basic, Premium]

context = Context({'site':Site()})

I was trying to display this class structure in a template like so:

{% for bundle in site.bundles %}
    <ul>
        {% for service in site.services %}
            {% if service in bundle.services %}
                <li><img src="images/checkmark.png" alt="Included!"></li>
            {% else %}
                <li>&mdash;</li>
            {% endif %}
        {% endfor %}
    </ul>
{% endfor %}

If that example isn't very clear, I've also attached the simplest possible test case I could come up with that I think illustrates the problem. At least, it fails on SVN and I think it is representative of my code. I'm unable to spot the bug in the smartif.py parser, if it's there.

Attachments (1)

smart_if_broken_in.diff (1.1 KB) - added by donspaulding 3 years ago.
smart-if-test-case

Download all attachments as: .zip

Change History (4)

Changed 3 years ago by donspaulding

smart-if-test-case

comment:1 Changed 3 years ago by donspaulding

  • Cc donspaulding added
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 3 years ago by lrekucki

  • Resolution set to invalid
  • Status changed from new to closed

Classes are callable, so what you're actually doing is checking if an *instance* is in a list of classes. See: http://docs.djangoproject.com/en/dev/releases/1.3/#callables-in-templates

comment:3 Changed 3 years ago by donspaulding

Ouch. Unpythonic template language is unpythonic.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.