[novice question] Render a list of objects

30 views
Skip to first unread message

Sudeep Gopal

unread,
Jun 21, 2019, 6:58:21 PM6/21/19
to Django users
Hello, 

I am new to django. 

I am using django forms in my project and I am not using django models. 

From the user,  my django form asks for 4 inputs :- signal_to_noise_1 , signal_to_noise_2, bandwidth1 and bandwidth2. 

Based on these values I use my  look up table and I create a results which are 2 lists of records.
 Each list has approximately 40 to 50 records. Each record has 8 fields. (field1,...field8) ...

Each list looks like this (if my description is not clear) field1 is unique. 

field1 field2 field3



field8











Both the lists (with each list having about 50 records) are in my django view. 

From what I read online, I can use the render() method which has a dictionary called context. (https://docs.djangoproject.com/en/2.2/ref/templates/api/#rendering-a-context)

I am not sure how to convert these list of record objects to this dictionary format which has {"name":value} pair. 

Even the django tables requires it in the dictionary format.....


But I did not understand what exactly the below syntax does and how I can customize it to my usecase ....any input would be greatly appreciated ...

<table>
  {% for r in results %}
    {% cycle '<tr>' '' '' '' %}
      <td>{{r.content}}</td>
    {% cycle '' '' '' '</tr>' %}
  {% endfor %}
</table>




Joe Reitman

unread,
Jun 22, 2019, 9:03:19 AM6/22/19
to Django users
The example code you provided will create a <tr> on the first iteration of the loop and </tr> tag every 4th iteration of the loop. This cycle will continue resulting in a table with 4 columns of data. Example:

results = [ 1, 2, 3, 4, 5, 6, 7, 8 ]


<table>
  <tr>
    <td>1</td>
    <td>2</td>
    <td>3</td>
    <td>4</td>
  </tr>
  <tr>
    <td>5</td>
    <td>6</td>
    <td>7</td>
    <td>8</td>
  </tr>
</table>


If you have 2 lists, render them from your view like return render(request, 'polls/detail.html', {'list1': list1, 'list2': list2})
In your template create an 8 column table:
<table>
  {% for r in list1 %}
    {% cycle '<tr>' '' '' '' '' '' '' ''%}
      <td>{{r}}</td>

    {% cycle '' '' '' '' '' '' '' '</tr>' %}
  {% endfor %}
{% for r in list2 %}
    {% cycle '<tr>' '' '' '' '' '' '' ''%}
      <td>{{r}}</td>

    {% cycle '' '' '' '' '' '' '' '</tr>' %}
  {% endfor %}
</table>

Hope this helps

Sudeep Gopal

unread,
Jun 23, 2019, 8:15:58 PM6/23/19
to Django users
Thank you so much Joe...

I was actually able to go forward. 

With the below code  :-

<table>
  {% for r in list1 %}
    {% cycle '<tr>' '' '' '' '' '' '' ''%}
      <td>{{r}}</td>
    {% cycle '' '' '' '' '' '' '' '</tr>' %}
  {% endfor %}


The {{r}} in the above code , gives only the reference or the pointer value. For eg, template cannot recognize it is field1 which I want to display or something,,,
How do we get the value of that reference ?

My result look like this :-

field1 field2 field3 field4 field5 field6 field7
<TDMAsearch.TDMAsearchutils.TDMAInroute object at 0x0000000005393FD0> <TDMAsearch.TDMAsearchutils.TDMAInroute object at 0x000000000581B550> <TDMAsearch.TDMAsearchutils.TDMAInroute object at 0x000000000581B278> <TDMAsearch.TDMAsearchutils.TDMAInroute object at 0x0000000005413908> <TDMAsearch.TDMAsearchutils.TDMAInroute object at 0x00000000053C27F0> <TDMAsearch.TDMAsearchutils.TDMAInroute object at 0x00000000053C20B8> <TDMAsearch.TDMAsearchutils.TDMAInroute object at 0x00000000053C2278>
<TDMAsearch.TDMAsearchutils.TDMAInroute object at 0x0000000005594F28> <TDMAsearch.TDMAsearchutils.TDMAInroute object at 0x0000000005413668> <TDMAsearch.TDMAsearchutils.TDMAInroute object at 0x0000000005413358> <TDMAsearch.TDMAsearchutils.TDMAInroute object at 0x0000000005413DA0> <TDMAsearch.TDMAsearchutils.TDMAInroute object at 0x00000000054DDF98> <TDMAsearch.TDMAsearchutils.TDMAInroute object at 0x00000000053C2978> <TDMAsearch.TDMAsearchutils.TDMAInroute object at 0x00000000055943C8>


I want it to look at the content 

CARRIER1 230 128 QPSK 1000 125 155
CARRIER2 230 256 QAM 1000 125 155

Sudeep Gopal

unread,
Jun 24, 2019, 9:12:38 AM6/24/19
to Django users
Update .... This worked for me..Thanks a lot for your help....:- 

<table> <th>FIELD1</th> <th>FIELD2</th> <th>FIELD3</th> <th>FIELD4</th> <th>FIELD5</th> <th>FIELD6</th> <th>FIELD7</th> <th>FIELD8</th> {% for r in ret_list %} <tr> <td> {{r.field1}} </td> <td> {{r.field2}} </td> <td> {{r.field3}} </td> <td> {{r.field4}}</td> <td> {{r.field5}} </td> <td> {{r.field6}} </td> <td> {{r.field7}} </td> <td> {{r.field8 }} </td> </tr> {% endfor %} </table>
Reply all
Reply to author
Forward
0 new messages