Opened 14 years ago

Closed 14 years ago

Last modified 14 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: dev
Severity: Keywords:
Cc: donspaulding Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

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 14 years ago.
smart-if-test-case

Download all attachments as: .zip

Change History (4)

by donspaulding, 14 years ago

Attachment: smart_if_broken_in.diff added

smart-if-test-case

comment:1 by donspaulding, 14 years ago

Cc: donspaulding added

comment:2 by Łukasz Rekucki, 14 years ago

Resolution: invalid
Status: newclosed

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 by donspaulding, 14 years ago

Ouch. Unpythonic template language is unpythonic.

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