Note that there are some explanatory texts on larger screens.

plurals
  1. POCan variables with lookups be used in Django {% ifequal %} expressions?
    primarykey
    data
    text
    <p>Here's my code:</p> <pre><code>[{{thing.syncedthing_set.all.0.state}}] {% ifequal thing.syncedthing_set.all.0.state "InProgress" %} It worked {% endifequal %} </code></pre> <p>The result:</p> <pre><code>[InProgress] </code></pre> <p>In other words, the variable lookup works, but isn't passing the 'ifequal' test. I can't think of any reason this wouldn't work other than variable lookups not being supported.</p> <p>There's a mailing list message from 2007 saying they weren't supported back then: <a href="https://groups.google.com/forum/?fromgroups#!topic/django-users/oU1im3UTcLc" rel="nofollow noreferrer">https://groups.google.com/forum/?fromgroups#!topic/django-users/oU1im3UTcLc</a> - is this still the case?</p> <p>If so, any good workarounds? It's not easy in this case to modify the Python code that calls the template.</p> <p>If they ought to work - any other possible causes?</p> <p>There's <a href="https://stackoverflow.com/questions/7509435/issue-with-django-template-ifequal">this very similar question</a> but the answer doesn't work for me. I'm using Django 1.3.</p> <p>EDIT</p> <p>Ok, so the 'state' has type class. Looking up the definition, state refers to a class which eventually subclasses models.Field:</p> <pre><code>class FSMField(models.Field): __metaclass__ = models.SubfieldBase states = {} """ Contains { 'Statename': StateClass, ... }""" def __init__(self, *args, **kwargs): kwargs.setdefault('max_length', 50) super(FSMField, self).__init__(*args, **kwargs) def to_python(self, value): if isinstance(value, State): return value return self.__class__.states.get(str(value))() def get_prep_value(self, value): return str(value) def get_internal_type(self): return 'CharField' </code></pre> <p>Given that - I'm actually not sure how to get a real string value in the templating system. Forgive my newbishness.</p> <p>EDIT2 Actually, the relevant bit is probably the 'State' class itself:</p> <pre><code>class State(object): """ A simple finite statement machine abstract class, with on_entry and on_exit events.""" def __unicode__(self): return "%s" % (self.__class__.__name__) def __str__(self): return "%s" % (self.__class__.__name__) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload