Using a key from a for loop in django - django

I have an array of arrays that I am trying to put into a table. It's something that works easily in PHP but I can't seem to figure out how to do it in Django. I want to do something like this:
{% for o in test %}
<tr>
<td>{{ test.row{{o}}.test1 }}</td>
<td>{{ test.row{{o}}.test2 }}</td>
<td>{{ test.row{{o}}.test3 }}</td>
</tr>
{% endfor %}

for an array like this :
random_array = [[1, 2, 3],[4, 5, 6]]
in python you should use
array[1][0] to acces the number 4
in django templating language it goes like this :
{{ random_array.1.0 }}
update :
<table>
{% for row in array %}
<tr>
{% for i in row %}
<td>{{ i }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>

Related

how to get random 1/2 data from database in jinj2 template in Django?

<table>
{% for field in fields %}
<tr>
<td>{{ field.name }}</td>
<td>{{ field.value }}</td>
</tr>
{% endfor %}
</table>
here we will get all the data from fields . but i want to only get randomly 1/2 (i can specified how many) data in jinja2 template from backend ?
How to do this ?
<table>
{% for field in fields %}
{% if forloop.counter < x %}
<tr>
<td>{{ field.name }}</td>
<td>{{ field.value }}</td>
</tr>
{% endif %}
{% endfor %}
</table>
just put your desired number in x,you are good to go.
Try this. You can add this logic to the frontend. This way it will display records with even ID. and those will 1/2 as well.
<table>
{% for field in fields %}
<tr>
{% if field.id%2 == 0 %}
<td>{{ field.name }}</td>
<td>{{ field.value }}</td>
{% endif %}
</tr>
{% endfor %}
</table>

How to index in Django Templates

Im trying to index a 2D list, whenever I access members.0, for example, the positioning works just fine. However I need this to change with the loop so I made a variable, but, if I was to do members.counter this wouldn't do anything.
Is there another way of doing this or is it not possible?
<tbody>
{% for item in projects %}
{% with counter=forloop.counter0 %}
<tr>
<td>{{ item }}</td>
<td>{{ members.counter }}</td>
</tr>
{% endwith %}
{% endfor %}
</tbody>
You can use the square brackets [] if you need to access a list or dict at a specific position:
<tbody>
{% for item in projects %}
{% with counter=forloop.counter0 %}
<tr>
<td>{{ item }}</td>
<td>{{ members[counter] }}</td>
</tr>
{% endwith %}
{% endfor %}
</tbody>

how to display retrieved data from database using django

i have a django project that is connected to SQL Server i tried to retrieve data from the database.
if i try to display all the data it run correct and display all the values.
but if i try to display the data in an html table it doesn't display anything.
views.py
def connect(request):
conn = pyodbc.connect(
'Driver={ODBC Driver 17 for SQL Server};'
'Server=DESKTOP-LPD1575\\SQLEXPRESS;'
'Database=testDB;'
'UID=test;'
'PWD=test;'
)
cursor = conn.cursor()
c = cursor.execute('SELECT * FROM Artist')
return render (request,'connect.html',{"c":c})
connect.html
{% for row in c %}
{{ row }}
{% endfor %}
this code in the html template work and display the data.
but if i try to do as below it will not work
<table align = "center">
<tr align="center">
<th>ID</th>
<th>FolderNumber</th>
<th>Title</th>
</tr>
{% for row in c %}
<tr align="center">
<td>{{ row.id }}</td>
<td>{{ row.artistName }}</td>
<td>{{ row.activeFrom }}</td>
</tr>
{% endfor %}
</table>
anyone can help me?
Each row in your result is a list, not a dict. You would need to use indexes not keys:
{% for row in c %}
<tr align="center">
<td>{{ row.0 }}</td>
<td>{{ row.1 }}</td>
<td>{{ row.2 }}</td>
</tr>
{% endfor %}
or better
{% for row in c %}
<tr align="center">
{% for item in row %}
<td>{{ item }}</td>
{% endfor %}
</tr>
{% endfor %}
However, really you should not be running SQL queries directly, but define a model for your table and access it that way.

How to add two for loops in a single table body in django template

<thead>
<th> username </th>
<th>place</th>
</thead>
{% for i, j in user_group1, user_group2 %}
<tbody>
{% if i %}
<td>{{ i.username }}</td>
<td>{{ i.place }}</td>
{% else %}
<td>{{ j.username }}</td>
<td>{{ j.place }}</td>
{% endif %}
</tbody>
{% endfor %}
I want to use two for loops in a sinle table body. First i need to start the first one and after that i need to start the next one. how can i do this
If you're using Jinja2, you can join the two lists into one with the + operator:
{% for i in user_group1|list + user_group2|list %}
<tbody>
<td>{{ i.username }}</td>
<td>{{ i.place }}</td>
</tbody>
{% endfor %}

using boolan in django templates

for item in query_results:
num +=1
print num
this will give you 1, 2, 3, 4 etc
I then tried doing this in django templates doing the following
{% for item in query_results %}
<tr>
<td>{{ item.user }}</td>
<td>{{ num|add:"1" }}</td>
</tr>
{% endfor %}
But this only returns 1, 1, 1, 1, 1 etc. This says to me that the 1 isn't being saved to num each cycle. IS this then not a capability of django templates, or am i just doing it wrong.
Use forloop.counter instead.
{% for item in query_results %}
<tr>
<td>{{ item.user }}</td>
<td>{{ forloop.counter }}</td>
</tr>
{% endfor %}
The built-in add filter just adds the argument to the value, but doesn't modify it. That's why you're getting always 1 as result.
More about it: https://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#add