You have a list of values.  Use the Pythonic way to iterating over a list, 
which does not involve using indices.

{% for o in graphobject %}
                {{ o }}<br />
                {{ o.subfield }}
{% endfor %}


From: [email protected] [mailto:[email protected]] On 
Behalf Of Bill Torcaso
Sent: Friday, April 20, 2018 8:50 AM
To: Django users
Subject: Re: Use self-increasing arguments in numeric for-loop in Django


I an new-ish to Django, and I ask this to hear from more experienced users 
whether this would work at all, and whether it would be considered a good or 
bad practice.

Goal: given an object and an integer index, retrieve the sub-object at that 
index within the incoming object.

Method:  Write a custom tag.  Provide the tag with the larger object, and the 
integer index.  Return the sub-object at that index (or whatever field(s) are 
needed).

Usage:


  {% for i in metarange %}

    {% graphobject_get_by_index graphobject {{ i }} %}

  {% endfor %}


In this particular case, there is an issue about whether the sub-object can be 
null, for some definition of null.  There are various ways to deal with that.

Again, I am new-ish to Django.  Is there a similar approach in which 
'get_by_index' is a method on class Graphobject, such that this would work?

    {% graphobject.get_by_index {{ i }} %}

Thanks for any explanation about these approaches.


On Thursday, April 19, 2018 at 12:01:37 PM UTC-4, 
[email protected]<mailto:[email protected]> wrote:

I am currently working on django.

I have a list of values which stored in 'graphobject', and 'metarange' is 
defined as range(0,59). In that case, how could I use numbers as argument to 
display the value stored in graphobject? I tried using following codes but it 
doesn't work



{% for i in metarange %}

{% if graphobject.i != '' %}

{{ graphobject.i }}

{% endif %}

{% endfor %}



Please tell me how could I do this?
--
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to 
[email protected]<mailto:[email protected]>.
To post to this group, send email to 
[email protected]<mailto:[email protected]>.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/9c162dd2-201b-4370-bac0-f3afb3db163a%40googlegroups.com<https://groups.google.com/d/msgid/django-users/9c162dd2-201b-4370-bac0-f3afb3db163a%40googlegroups.com?utm_medium=email&utm_source=footer>.
For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/62ae273ee7634261ad5421b0809c28cc%40ISS1.ISS.LOCAL.
For more options, visit https://groups.google.com/d/optout.

Reply via email to