Opened 8 years ago

Closed 8 years ago

Last modified 8 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: no UI/UX: no


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 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):

class EmailSupport(Service):

class LiveSupport(Service):

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 %}
        {% for service in %}
            {% if service in %}
                <li><img src="images/checkmark.png" alt="Included!"></li>
            {% else %}
            {% endif %}
        {% endfor %}
{% 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 parser, if it's there.

Attachments (1)

smart_if_broken_in.diff (1.1 KB) - added by donspaulding 8 years ago.

Download all attachments as: .zip

Change History (4)

Changed 8 years ago by donspaulding

Attachment: smart_if_broken_in.diff added


comment:1 Changed 8 years ago by donspaulding

Cc: donspaulding added

comment:2 Changed 8 years ago by Łukasz Rekucki

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:

comment:3 Changed 8 years ago by donspaulding

Ouch. Unpythonic template language is unpythonic.

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