Use self-increasing arguments in numeric for-loop in Django

27 views
Skip to first unread message

shaw...@gmail.com

unread,
Apr 19, 2018, 12:01:37 PM4/19/18
to Django users

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?

Jani Tiainen

unread,
Apr 19, 2018, 12:29:18 PM4/19/18
to django...@googlegroups.com
Hi. Django templating language isn't programming language. It can't do that. You need to prepare data suitable for displaying in your view.


--
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 django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
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/931f17b7-4cca-4b11-9aae-7e3092732dd3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Jani Tiainen

- Well planned is half done, and a half done has been sufficient before...

Matthew Pava

unread,
Apr 19, 2018, 12:32:31 PM4/19/18
to django...@googlegroups.com

Is there something stopping you from using this construct?

{% for item in graphobject %}

 

From: django...@googlegroups.com [mailto:django...@googlegroups.com] On Behalf Of Jani Tiainen
Sent: Thursday, April 19, 2018 11:29 AM
To: django...@googlegroups.com
Subject: Re: Use self-increasing arguments in numeric for-loop in Django

 

Hi. Django templating language isn't programming language. It can't do that. You need to prepare data suitable for displaying in your view.

 

On Thu, Apr 19, 2018 at 7:01 PM, <shaw...@gmail.com> 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 django-users...@googlegroups.com.



 

--

Jani Tiainen

 

- Well planned is half done, and a half done has been sufficient before...

--

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 django-users...@googlegroups.com.


To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

shaw...@gmail.com

unread,
Apr 19, 2018, 12:35:37 PM4/19/18
to Django users
As I said, the data is already stored in variable: graphobject, I can simply achieve my goal by coding like this: {{% graphobject.1%}}, {{% graphobject.2 %}} ...... But I want a loop to do that

在 2018年4月19日星期四 UTC+2下午6:29:18,Jani Tiainen写道:
Hi. Django templating language isn't programming language. It can't do that. You need to prepare data suitable for displaying in your view.

On Thu, Apr 19, 2018 at 7:01 PM, <shaw...@gmail.com> 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 django-users...@googlegroups.com.

Jani Tiainen

unread,
Apr 19, 2018, 12:42:31 PM4/19/18
to django...@googlegroups.com
Hi again,

and as I said, Django templating language is not a programming language and it doesn't support it (it's by design).

If your graphobject is a list of items, use iterating it over as Matthew Pava suggested in his reply.

Otherwise you need to either create custom tag/filter that returns what you want, or change dataformat suitable for displaying and processing in template.

To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

For more options, visit https://groups.google.com/d/optout.

Bill Torcaso

unread,
Apr 20, 2018, 9:50:00 AM4/20/18
to Django users

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.

Matthew Pava

unread,
Apr 20, 2018, 9:54:22 AM4/20/18
to django...@googlegroups.com

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: django...@googlegroups.com [mailto:django...@googlegroups.com] 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.

--

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 django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Peter of the Norse

unread,
May 3, 2018, 10:37:49 AM5/3/18
to django...@googlegroups.com
First of all, this is a terrible idea.  You should fix up graphobject in your view to do what you want, not try to massage it in your template.

Second, you can’t use template tags inside a template.  That’s not how it works at all.

Third, you are not the first to have this terrible idea.  You can search the web and find many examples of people “solving” this non-problem.

- Peter of the Norse
--
Reply all
Reply to author
Forward
0 new messages