how to display retrieved data from database using django - 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.

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 print only certain cells of my table in Django templates?

I am developing an app in Django.
I have on my template (code 0):
{% for row in query_result %}
<tr>
{% for cell in row %}
<td>{{ cell }}</td>
{% endfor %}
</tr>
{% endfor %}
Let's suppose I want my template to print only the first and the third column of my matrix, how can I indicate to my template a specific cell?
I tryed with
{% for row in query_result %}
<tr>
<td >{{ cell[0] }}</td>
<td >{{ cell[2] }}</td>
</tr>
{% endfor %}
but it does not work.
EDIT:
As suggested, I tryed with (code 2):
{% for row in query_result %}
<tr>
<td >{{ cell.0 }}</td>
<td >{{ cell.2 }}</td>
</tr>
{% endfor %}
But this somehow erases the content of my cells, see here below.
results of Code 0:
results of code 2:
You can obtain an item at an index in Django's template engine by fetching it as you would fetch an attribute, so:
{% for row in query_result %}
<tr>
<td >{{ row.0 }}</td>
<td >{{ row.2 }}</td>
</tr>
{% endfor %}
That being said, I strongly advise to try to fix the problem upstream: instead of rendering a subset, take a look what you can do to limit the "columns" in query_result.
or you can unpack the elements and put the second item in a "throwaway" variable:
{% for cell0, __, cell2 in query_result %}
<tr>
<td >{{ cell0 }}</td>
<td >{{ cell2 }}</td>
</tr>
{% endfor %}
Use looks like {{ cell.0 }} this. The Django docs explain in the section variables and lookups.
So you would do something like:
{% for row in query_result %}
<tr>
<td >{{ cell.0 }}</td>
<td >{{ cell.2 }}</td>
</tr>
{% endfor %}

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 %}

Rendering dictionary having values in list format in django template

I have dictionary formate like :
uri_dict :{
"152.2.3.4" : ["/v1/draft" , 2],
"172.31.13.12" : ["/v1/url" , 34]
}
I want to render this in django template in tabular formate:
{% for keys, value in url_dict.items %}
<tr border = 1px black>
<th>{{ keys }}</th>
<td> {{ value[0] }} </td> <!--requires value[0] but not working -->
<td>{{ value[1]}} </td> <!--not working -->
</tr>
{% endfor %}
Please provide me any solution--- how to iterate over list values in template?
How to iterate over list values
There are a few options:
<td>{{ value.0 }}</td>
<td>{{ value.1 }}</td>
{% for item in value %}
<td>{{ item }}</td>
{% endfor}
These two are covered in my comment (and the other answers after it). For a list of length 2 you can also:
<td>{{ value|first }}</td>
<td>{{ value|last }}</td>
To access array elements on a django template you must refer to them as elem.attribute.
In your case, value.0 and value.1.
{% for keys, value in url_dict.items %}
<tr border = 1px black>
<th>{{ keys }}</th>
<td>{{ value.0 }}</td> <!--requires value[0] but not working -->
<td>{{ value.1 }}</td> <!--not working -->
</tr>
{% endfor %}
This page may help you: How to access array elements in a Django template?
Hope this helps,
If you always have only two items on the each value of dictionary, which are lists, then
{% for keys, value in url_dict.items %}
<tr border = 1px black>
<th>{{ keys }}</th>
<td> {{value.0}}</td>
<td> {{value.1}}</td>
</tr>
{% endfor %}
If there can be any number of items on list, then just loop for each items:
{% for keys, value in url_dict.items %}
<tr border = 1px black>
<th>{{ keys }}</th>
{% for eachval in value %}
<td> {{ eachval}}</td>
{% endfor %}
</tr>
{% endfor %}

Django: Printing a queryset dynamically in the template

How can I print the columns specified in "fields_i_want" instead of hardcoding the column names in the template code?
# Let's say I have this in my view:
foo = Foo.objects.filter(some_field='bar').select('field1', 'field2', 'field3')
fields_i_want = ['field1', 'field2']
# Then in the template I want to do something like this:
<TABLE id="some_id">
<TBODY>
{% for row in some_var.foo %}
<tr>
{% for value in another_var.fields_i_want %}
<td>{{ row[value] }}</td>
{% endfor %}
</tr>
{% endfor %}
</TBODY>
</TABLE>
# Instead of doing this:
<TABLE id="some_id">
<TBODY>
{% for row in some_var.foo %}
<tr>
<td>{{ row.field1 }}</td>
<td>{{ row.field2 }}</td>
</tr>
{% endfor %}
</TBODY>
</TABLE>
See this SO question.