Re: Conditionals in Templates
Here's a workaround I came up with. I defined a class: class TableValue: def __init__(self, value): self.value = value self.is_list = isinstance(value, list) Then in the Template I wrote {% for row in rows %} {% for cell in row %} {% if cell.is_list %} {% for element in cell.value %} {{ element }} {% endfor %} {% else %} {{ cell.value }} {% endif %} {% endfor %} {% endfor %} Is that considered decent Django? On Tuesday, July 3, 2012 12:50:15 PM UTC-7, Russ Abbott wrote: > > I'm generating a table. I want the cells to be different depending on the > type of the content. How do I test for the type of the content? I'd like to > do something like this. > > > {% for row in rows %} > > {% for cell in row %} > > {% if isinstance( {{ cell }}, list) %} > > {% for element in cell %} > {{ element }} > {% endfor %} > > {% else %} > {{ cell }} > {% endif %} > > {% endfor %} > > {% endfor %} > > > But "{% if isinstance( {{ cell }}, list) %}" is not allowed. Apparently I > can't call a function in an {% if ... %} tag. Is there another way to do > this? > > Thanks. > -- You received this message because you are subscribed to the Google Groups "Django users" group. To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/HxfDQVmchj0J. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Conditionals in Templates
I'm generating a table. I want the cells to be different depending on the type of the content. How do I test for the type of the content? I'd like to do something like this. {% for row in rows %} {% for cell in row %} {% if isinstance( {{ cell }}, list) %} {% for element in cell %} {{ element }} {% endfor %} {% else %} {{ cell }} {% endif %} {% endfor %} {% endfor %} But "{% if isinstance( {{ cell }}, list) %}" is not allowed. Apparently I can't call a function in an {% if ... %} tag. Is there another way to do this? Thanks. -- You received this message because you are subscribed to the Google Groups "Django users" group. To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/jRlkDDGDgk0J. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Newbie form question
As a Django newbie I apologize if this is a trivial question. I'd like to use a form field as an element in a generated page but not as part of a form. In particular, I'm generating a table, some of whose elements are text and others of which I want to be drop-down lists. I thought I might be able to use forms.ChoiceField to generate the drop-down lists. When I write ... choices = forms.ChoiceField([1, 2, 3]) # as a test case ... I get a django.forms.fields.ChoiceField object. But when I include it as an element of the table nothing appears. Is this a reasonable approach? (It seems very easy if it really could be made to work.) If so, what should I do to get it to display as a drop-down list? Thanks. -- You received this message because you are subscribed to the Google Groups "Django users" group. To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/U1l9_rTtkmQJ. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.