We will display details of three employees. For this we will create a class and then three objects to store data. Then we will display the data in a html file by using for loop.
We will create one class employ in our model.py file
model.py
from django.db import models
# Create your models here.
class employ(models.Model):
emp_id = int
emp_name = str
emp_desg = str
Using this class employ, we will create one function emp_display and inside that we will create three objects to store details of three employees.
from django.shortcuts import render
from .models import employ
# Create your views here.
from django.http import HttpResponse
def emp_display(request):
emp1=employ()
emp1.emp_id=123
emp1.emp_name='Ravi'
emp1.emp_desg='Manager'
emp2=employ()
emp2.emp_id=124
emp2.emp_name='Raju'
emp2.emp_desg='Driver'
emp3=employ()
emp3.emp_id=125
emp3.emp_name='Kiran'
emp3.emp_desg='Store Keeper'
em_all=[emp1,emp2,emp3]
return render(request,'display.html',{'em_all':em_all})
Here we have passed the details of the employees through a list and our main page display.html will display the list by looping. Here is the code for display.html.
{% for ems in em_all %}
{{ems.emp_id}}, {{ems.emp_name}},{{ems.emp_desg}}
{% endfor %}
To run all above codes we need to register our function inside urls.py file