Django template : For Loop

1,849 views
Skip to first unread message

Jagdeep Singh Malhi

unread,
Sep 5, 2010, 7:54:20 AM9/5/10
to Django users, greatde...@googlegroups.com
I have question about "for loop" used in Templates
the correct Syntax of using For Loop in template is :
{% for X in VALUE %}

but I want to use For loop with two value
Is it possible to use two value in one loop?
For example :
{% for X, Y in VALUE1, VALUE2 %}
Or
{% for X in VALUE1 && Y in VALUE2 %}

These syntaxes are wrong, just for example.
Is it possible to use for loop with two value in django templates?
if yes, How?

Thanks

Steve Holden

unread,
Sep 5, 2010, 8:16:12 AM9/5/10
to django...@googlegroups.com
If these objects are lists, say list1 and list2, in your view just
create a single object made up of pairs of elements from the original lists:

values = zip(list1, list2)

Then you can pass that item to your template and iterate over it.

In older Django you would have to use dot notation to select the elements:

{% for x in VALUES %}
First item is x.0
Second item is x.1
{% endfor %}

I believe in more recent versions you have the option of unpacking the
two elements in the for statement:

{% for x1, x2 in lists %}
...
{% endfor %}

but don't take that as gospel until you've tried it.

regards
Steve

--
DjangoCon US 2010 September 7-9 http://djangocon.us/

Jagdeep Singh Malhi

unread,
Sep 5, 2010, 12:29:51 PM9/5/10
to Django users

> If these objects are lists, say list1 and list2, in your view just
> create a single object made up of pairs of elements from the original lists:
>
>    values = zip(list1, list2)
>
> Then you can pass that item to your template and iterate over it.

I tried this, its not working.

Justin Myers

unread,
Sep 5, 2010, 1:34:14 PM9/5/10
to Django users
What error message is it giving you?

I'd assume it's a problem with the objects you're passing it, since
zip is a built-in Python function: http://docs.python.org/library/functions.html#zip

On Sep 5, 11:29 am, Jagdeep Singh Malhi <singh.malh...@gmail.com>
wrote:

Steve Holden

unread,
Sep 5, 2010, 1:45:42 PM9/5/10
to django...@googlegroups.com
Sorry, but "not working" does not constitute useful information.

My car isn't working. Can you fix it? ;-)

Jagdeep Singh Malhi

unread,
Sep 6, 2010, 1:01:38 AM9/6/10
to Django users

> > I tried this, its not working.
>
> Sorry, but "not working" does not constitute useful information.
>
> My car isn't working. Can you fix it? ;-)

model.py
from django.db import models
from django.forms import ModelForm

class Input(models.Model):
input1 = models.FloatField()
input2 = models.FloatField()

class Output(models.Model):
out = models.ForeignKey(Input)
output = models.FloatField()

class InputForm(ModelForm):
class Meta :
model = Input

class OutputForm(ModelForm):
class Meta :
model = Output

view.py
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response, get_object_or_404
from mysite.add_db.models import *
from django.template import RequestContext
from django.core.urlresolvers import reverse

def add_db(request):
if request.method == 'POST':
form = InputForm(request.POST)
if form.is_valid():
cd = form.cleaned_data
input1 = cd['input1']
input2 = cd['input2']
p = form.save()
form_output = OutputForm()
output = input1 + input2
form_output_final = p.output_set.create(output=output)
return render_to_response('add_db/output.html', {'form': form,
'input1':input1, 'input2':input2, 'output':output},
context_instance=RequestContext(request))
else:
form = InputForm()
return render_to_response('add_db/add.html', {'form': form},
context_instance=RequestContext(request))

def result(request):
input_list = Input.objects.all().order_by('-id')
output_list = Output.objects.all()
return render_to_response('add_db/result.html', {'input_list':
input_list, 'output_list': output_list})

Templates result.html
<h1> All Data </h1>
{% if input_list %}
<ul>
{% for input in input_list %}
{% for output in output_list %}
{% if input.id == output.id %}
<li>{{ input.id}} : {{ input.input1 }} + {{ input.input2 }} =
{{output.output}} </li>
{% endif %}
{% endfor %}
{% endfor %}
</ul>
{% else %}
<p>No data are available.</p>
{% endif %}

now i try this code

{% for input in input_list %}
{% for output in output_list %}
{% if input.id == output.id %}

my problem is solved
but is this correct way to get values from two tables and use in
templates?

Jagdeep Singh Malhi

unread,
Sep 6, 2010, 12:01:02 PM9/6/10
to Django users

> but is this correct way to get values from two tables and use in
> templates?

Me actually want to combine to tables and use there values in template
Is the above post code is correct way to do this?

Thanks

Tim Chase

unread,
Sep 6, 2010, 1:36:21 PM9/6/10
to django...@googlegroups.com, Jagdeep Singh Malhi

Combine the in the *view* with the zip() built-in, and use
Steve's suggestion to split the parts in the template (you can
use the tuple-unpacking version in Django 1.0 or later[1])

-tkc

[1]
http://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#for

Reply all
Reply to author
Forward
0 new messages