so here how the this list is coming ...... let us check what is going inside ....
so for this we need to see the pages ie.,
1. contact_list.html
2. model.py
ie., check down
contact_list.html
<h1>Contacts</h1>
<ul>
{% for contact in object_list %}
<li>{{contact}}</li>
{% endfor %}
</ul>
model.py
from django.db import models
class Contact(models.Model):
first_name = models.CharField(
max_length=255,
)
last_name = models.CharField(
max_length=255,
)
father_name = models.CharField(
max_length=255,
)
email = models.EmailField()
def __str__(self):
return ' '.join([
self.first_name,
self.last_name,
self.father_name,
])
so here check the red color highligted one
in the html page if we write contact in the filter that is which the list is coming
and in the model.py we wrote def __str__(self) means it is contact class only it will return and it is return with join for first name,last name and father name.
that is why it is coming......
so it means there u r getting first_name,last_name and father name write
now i want to make it different in table order................ ok before that let us try for different list order
wirte for that u need to do like this in html file ie.,
<ul>
{% for contact in object_list %}
<li>{{contact.father_name}}</li>
{% endfor %}
</ul>
so i got only father name
now i will make the table and check for that in html file.......... ok let do it
<h1>Contacts</h1>
<table border=1>
<tr><th>First Name</th><th>Last Name</th><th>Father Name</th></tr>
{% for contact in object_list %}
<tr>
<td>{{contact.first_name}}</td>
<td>{{contact.last_name}}</td>
<td>{{contact.father_name}}</td>
</tr>
{% endfor %}
</table>
now the output is
No comments:
Post a Comment