In the last topic we saw the Forms method and ORM Queries method where I showed you a theoretical video explaining to you which particular methods are of forms through which we can send data and what are the ORM Queries method through which we can fire our ORM queries.
Our today’s topic is CRUD Application with MySQL Database.
Meaning we are going to make a CRUD Application in Django in which we are going to learn about MySQL Database connectivity as well.
First of all, let’s know what CRUD Application is.
CRUD Application is Create, Read, Update & Delete.
We are going to perform these 4 operations in CRUD Application.
So what is the first thing that we have to do when making a CRUD Application?
First of all, we have to do connectivity with MySQL in Django.
But before that, you have to set up your Django.
I had shown and explained that to you when we saw the video of Create Application and Project.
If any of you don’t know it yet, you can go & check in that video, okay?
So what will we do first?
We will set up our project, right?
I will create a folder here named CRUD Application, right?
And open CMD here first of all, right?
After opening CMD, the first thing we have to do is we have to create a Virtual Environment.
We will make a Virtual Environment first.
Python minus m venv myvenv.
It will create my virtual environment.
I have already told you all of these things in the previous videos.
Those of you who don’t know, do watch the previous videos once so that you can know all of the things in detail.
So now we have to activate our Virtual Environment.
We will activate our Virtual Environment.
And then we will install our Django.
So our Django installation has started.
What is the command for Django installation?
Pip Install Django.
And then, after installing Django, you have to create a Django Project here.
Start project. And then project name, right?
I have kept the project only as the project name to make you understand.
Then we have to make our application.
Start app and then app.
I have kept our Application’s name as App only so it’s easier for you to understand.
And then you have to open your VS Code.
Your VS Code is open.
Then we have to go to Settings.py & install your application, right?
I have already told you the process in the previous videos. I’m telling you in each video so that you can understand easily.
Now what do we have to do here?
We have to go to urls.py.
Have to import Include here.
And then include your application url.
We’ll include it here.
And then app.urls, okay?
So this got included.
Now you have to go in Application.
Right click on the Application folder, New.
And make a urls.py file, right?
After that, you have to copy this particular portion and paste it here.
And put the urls part blank.
After doing this much, you will now do MySQL connectivity.
So how do you have to do MySQL connectivity?
First of all, you have to open whichever database software you have.
For Example, if I have SQLyog or if you have XAMPP, you can open that too.
Okay so you have to open it.
You have to create a Database in that.
And then you have to go to settings.py & paste your MySQL settings.
I’ll show you practically from where you have to copy it.
I opened my XAMPP here.
You have to click on New here.
Here you can give any database name, suppose I’m giving CRUD here.
Why? Because we are making a CRUD Application.
Now after creating a database, what you have to do is, you have to search on the browser only.
What will you search for?
MySQL settings for Django.
As soon as you search, you will find a link ‘How To Use MySQL or MariaDB with your Django Application.
When you go here, you will get these settings if you go a little below.
You will be able to find these settings here.
How much do you have to copy?
We have to copy from Default to our 2nd last bracket.
And you have to go to settings.py in your project.
In that, you will find your database section there.
See, by default Django gives SQLite3 but here, we have to work with MySQL.
So we will replace this part, the setting we copied, right?
And then what will you write in Name here?
You will write your database name, okay.
In User, your database username.
In Password, your Database User Password.
Your Host will be Local Host only.
If anyone has a port number & your local host is working along with it, then you’ll have to enter all of that here.
And Port number here, your MySQL Port number.
So what is the database that we made? CRUD.
What is our User? Root.
Our password? We had kept it blank.
And then the port number 3306.
If any of you don’t know how to search port numbers, when you open your XAMPP Control Panel, you can see your Port Number here, okay?
After doing this much, now when you go here & migrate it here.
So python manage.py migrate.
Now, as soon as you migrate, it will ask you a question which is, ‘Did you install MySQLclient?’
That means you have to install MySQLclient.
What do you have to do?
You have to install MySQLclient.
So how do you have to install?
Let me show you.
When you have to install MySQLclient, you have to follow this command which is pip install -- only - binary : all : mysqlclient.
How do you have to do it in CMD?
I’ll open CMD here, right?
And here you have to fire a command which is pip install -- only - binary : all mysqlclient.
Okay? As soon as you press Enter, it will install your MySQL client.
And then, after installing it, if you migrate, your tables will migrate and it will show in your database.
See, just as I clicked on CRUD, the 10 tables of Django got shown here.
That means that my Django is connected with MySQL.
So first of all, you have to do this much.
Now, what do you have to do?
You have to create a model here.
Meaning after creating a project and an app, you have to create a model with whose help, you can perform a CRUD operation.
How do we have to do it?
I have shown you a model here.
What you have to do is, you have to create a VS Code.
There is a models.py file here.
I made a class and gave it the name Student.
And then First Name. models.Character Field.
And then maxlength 50.
I will take 4 fields in this way.
I’ll update the variables here.
Which is Last Name.
I want the emails here.
And which field will be here? Email Field.
And then we have to make contact.
And for contact, what will you write here?
We will take BigIntegerField.
And always remember 1 thing, whenever you are taking Integer Field in Django, you don’t have to provide its length in its Integer Field.
After doing this, what do you have to do?
Go back to CMD, write python.py makemigrations.
It will create your model.
And then you have to migrate and it will get migrated.
Now when you go to the browser & click on refresh, this is your table & it got placed here.
Is it clear so far?
Now what do you have to do?
After creating this model, you have to follow this template folder structure.
In which you have to make a Template Folder, in the App.
I had told you all these things in the previous videos, when I taught you how to load Templates.
Those who haven’t seen it, kindly watch the previous Load Template on Browser video to understand easily.
What you have to do is, you have to make a Template Folder, in that an App folder, and you have to make these 3 HTML Files in it.
Let’s go to VS Code.
This is my app folder, right click, New, templates/app folder.
So our template/app folder is made.
Then Right click on App, we’ll make Index, sorry Insert.html.
Then one more file.
And we’ll also make edit.html.
So we made these 3 files here.
See, we followed this particular structure, right?
We made a Template folder, in that an App folder, and in that we made Insert, Show and Edit html.
Now, we need to follow particular functions which we have to make in Views, I’ll show everything practically to you.
First of all, we have to make an InsertPageView, through which you will render an InsertPage on the browser.
Then you have to create a View to insert Data, which you’ll name InsertView.
Then however much data you want to fetch, you can name that View as ShowView through which all of your data will be fetched.
And if you want to show it on your browser, then you need to make 1 more view, that is the 4th View, which is the Edit Page View.
In which we will fetch a single object data and show it on the browser.
Then the 5th View that we’ll make, will be the Update View with the help of which we’ll update our data in our database.
And our last view will be DeleteView with the help of which we will delete our object.
We have to work keeping these views in mind.
So what is our first functionality?
We have to insert data in our database.
So how do we use insert?
First of all, what we have to do is, we have to make our HTML form.
We have to go to insert.html.
I have taught you how to make a form, right? In the ‘How to create HTML Form’ video.
Watch it once to know how to create a form.
I’ll create here as well & show you, okay?
So that you can understand it easily.
First of all, we have to take the Center Tag here.
Why Center Tag?
Because we want the form in the Center, right?
After that, we have to take a form in the Post Method.
I have already explained this method as well, in the forms method video.
If any of you don’t know how the post method works, please watch that video first so that you can understand it easily.
We have to take the Post method.
In this form, we have to take a table, then TR, then TD.
We’ll take First TD for First Name, right?
Now see, our template should be according to our model.
How many fields do we have in the model?
First name, Last name, Email & Contact.
We will take that many fields only, which is TD, input, right?
Now we have to give a name.
Why do we have to give a name?
Because if you don’t give input name, then no data will go through its name.
Meaning if you want to send your data to the server, then it is compulsory to keep the name in input type.
Because with the help of the name only, your data will reach the server.
We have to copy the TR.
And then make 4 TR.
2nd TR is for Lastname where I gave Lname.
3rd is for Email whose type too we have to put email because it will access the email ID only, its name too we will keep Email.
And here Contact, type will remain text only, and we’ll take contact here.
If you want to take contact, you can input type as tel.
Tel is for telephone, right?
You have to remove TD from here.
We now need a button here.
Which type do we have for buttons?
We don’t want names etc right now, we only want value.
And we will name the value Insert.
We made a form.
Now we will render this form on the browser once.
So what do we have to do here to render?
We have to make a New View which is def InsertPageView.
I have already shown you the functions that we have to make in Views.py.
What will this function do?
It will render in your browser.
So request app/insert.html.
And then you have to remember a mantra. Okay?
I’ll tell you in an easy way that for all the functions that you make, you have to make urls in the app’s urls.py.
So you’ll open the urls.py of the app here, from . import Views.
You’ll import Views, right?
And firstly, give a path which will render our insert form on the browser.
Which is views.InsertPageView, our function’s name.
And then I gave the name InsertPage here, right?
And now we will run our server, so let’s go to CMD.
I applied that & our server started running.
Let’s go to the browser.
127 & our form is ready.
Okay, we haven’t given a heading here.
How will we give the heading?
We shall go back to the Insert Template, right?
And here in the H1 tag, we will write Insert Form.
And then when we refresh it, we got the heading as well.
So our form is ready now.
Now, we have to insert data in our database through this particular form.
How will we do it?
We’ll go back to Views.py, right?
Now, first and foremost, we will make one more function which will insert.
Def Insert Data, okay?
I made a View function.
We passed a request here.
Now, our HTML, meaning the data we data we are receiving through Input, we have to bring it in views.py.
How will we bring it?
We’ll take a variable here, suppose I took a variable fname & we are getting a request from the .Post method.
See, why did I write Post? Because our form’s method is Post.
Right? So it is compulsory to write Post.
And then, we will write fname here.
Which is this fname? The one we had given in input name, right?
Remember these things.
Because, otherwise you will get confused and your data won’t be inserted.
In the same way, we’ll take Lname.
Request.post and then Lname.
In the same manner, our email.
So what we did was, we brought our data on the server.
Data comes from HTML to View.
So what we did was, we brought our data from HTML to View.
Now we have to create an object.
Creating Object of Model Class.
We will create an object now so we want to store our data in a particular database.
For that, we will have to import models.
From.models import * (star)
What does star mean?
It means that you will bring whichever classes are made in models.py.
We imported models here.
So I made a variable named newuser, right?
Then first thing we have to do is insert data
So we will write Inserting Data into Table.
What do we have to do?
What is our table name?
It is Student, right? Our class’ name.
We had seen the create method, in the previous video of ORM query method.
In that I had told you why we use the create method?
To insert the data.
Now in the create method, we have to pass the attribute, meaning we have to pass the arguments.
How do we have to pass the arguments?
From where do we have to pass the first argument?
We have to pass the variable names that we have in models first of all.
Why? Because we want to store the first name.
And then fname.
Where did the first name come from?
Where did the variable names that we took in models.py and this fname come from?
We took the variable names from the Views, right?
And then, in the same way, last name from models and then = the variables we took from Views that.
Then in the same way, email, which is in model.
=email which is in Views.
And again, in the same way, our contact which is coming from models first & then contact which is coming from View.
You will get to know these things, okay?
So we wrote everything here.
Now we have to open the Show Page here.
Just as the data gets inserted, a new page should open up.
On what page are we sending? On Show page.
So After Insert, Render on Show.html.
How do we have to do it?
Return Render Request App/show.html.
Now you created a view.
Made a new view so now what do you have to do?
You have to go to urls.py and make a new url.
We will take a path here and you have to then pass a value insert.
Why? Because you can’t put two blanks.
So you have to pass a value once. Okay?
Then we have to call views.InsertData.
And write name here.
So I haven’t explained the meaning of name until now.
What does name mean?
This name will tell this particular url that it has to call that specific view through this name.
So we will copy this name.
And save it under insert.html in this specific pattern.
You have to write a url and this.
Whatever we have written here is called the Django pattern.
It is called Django’s Jinja technique.
Django’s Jinja technique, right?
It is compulsory to write action.
Meaning what will this action do?
It will call InsertView and your data will be inserted through that view.
Now, always remember one thing: whenever you are taking that form, it is compulsory to write CSRF token in this form.
Why CSRF token?
It is compulsory to write CSRF token so that your data stays secured in Django.
If you don't write a CSRF token in the form, no matter how many forms you make, if you don’t write a CSRF token in each form, your data won’t be there in the sent database.
So we wrote CSRF token.
Our server is in run mode only.
We’ll refresh it and write our data here.
Sunit. Wrote my name here.
Wrote Email ID. And inserted by data here.
Now see, I will show a simple message on the show page, which is Show Page.
So that we can know what our Show Page has been called.
See, it got inserted and the Show Page is also seen.
And then when we refresh, we can see that our data has been saved.
So this is how you can insert data into the database into Django.
If you have any queries or comments, click the discussion button below the video and post there. This way, you will be able to connect to fellow learners and discuss the course. Also, Our Team will try to solve your query.
So next time when we meet, in the next video, we will learn about how to fetch data that is show data on the browser in Django.
So it will be continued in this project only.
The ENGINE variable specifies the SQLite driver, as can be seen. We need to follow a simple setup to use MySql as the backend engine for a Django project: MySql Server should be installed (we can also use a remote one) Install the Mysql Python driver, which Django uses to connect to and communicate with MySQL.
Due to its speed, versatility, and durability, MySQL is a prominent open source database management system used in online applications. For accessing and processing data in databases, MySQL uses SQL, or Structured Query Language.
Django is a Python web framework that may be used to create sophisticated online applications.
where will i get code access for the project source code?
this course structure is very good. I suggest for learning to this website.
Superb and amazing 😍🤩 enjoyable experience.
Er Komal Gupta
thnku so much sir. after waisting so mch time in boring videos.i learned only from here.explained vry well and i will suggest evryone that learnvern is the bst online plateform where you can learnalot
ajax lesson was something like boring . But , learned a lot with the sunit sir . Just learned how to deal with ajax in django