Django: Inserting multiple rows to MySQL table


Data Source

We have 35 rows of student data available in query format to insert to our table. The table is already created using migration here.

The list is created using student data from here. The text file can be used to create the list l1.

views.py
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.

models.py
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
Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate 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.



Subscribe to our YouTube Channel here



plus2net.com







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 Contact us
©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer