Django Data display by for loop

Django Django MySQL Database
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
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'),
]

Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer