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
from django.urls import include, path
from.import views
urlpatterns = [
path('',views.home,name='home'),
path('student/add',views.add_data,name='add_data'),
path('student/display',views.emp_display,name='emp_display'),
]
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.