from django.shortcuts import render
from django.http import HttpResponse
from .models import student
from django.db import transaction
def insert_all(request):
student.objects.all().delete() # remove all records
l1=[(1, 'John Deo', 'Four', 75, 'female'),
(2, 'Max Ruin', 'Three', 85, 'male'),
(3, 'Arnold', 'Three', 55, 'male'),
----
----
(34, 'Gain Toe', 'Seven', 69, 'male'),
(35, 'Rows Noump', 'Six', 88, 'female')]
for st in l1:
en=student(id=st[0],name=st[1],cl=st[2],gender=st[4],mark=st[3])
en.save()
str1="Records added with last id:" + str(en.id)
return render(request,'signup_student.html',{'msg':str1})
From models we have imported the student class. from django.db import models
# Create your models here.
class student(models.Model):
name =models.CharField(max_length=100)
cl=models.CharField(max_length=10,db_column='class')
mark =models.IntegerField()
gender =models.CharField(max_length=6)
class Meta:
db_table='student'
Inside our views.py ( code above ) we have imported student class
from .models import student
First we are removing the records by using the student class
student.objects.all().delete() # remove all records
In above code we are looping through the list of students and then inserting each rocord to our student table.
for st in l1:
en=student(id=st[0],name=st[1],cl=st[2],gender=st[4],mark=st[3])
en.save()
Getting the last row inserted id.
str1="Records added with last id:" + str(en.id)
return render(request,'signup_student.html',{'msg':str1})
The code at signup_student.html is here. We placed our variable str1 inside the template.
{% extends 'temp_basic.html' %}
{% block content %}
{{msg}}
<form action='signup_student_output' method=POST>
{% csrf_token %}
name :<input type=text name=name><br>
class :<input type=text name=class><br>
Mark :<input type=text name=mark><br>
Gender :<input type=text name=gender><br>
<input type=submit value=Submit>
</form>
{% endblock %}
Data display Adding single record to MySQL table
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.