Hello guys, welcome to learnvern.
The Last topic we saw was about displaying data on the browser using django. Which means in the last topic, we displayed the data dynamically from the database table on the browser by using bootstrap table. okay? Today we will see how to update the data that we have inserted and displayed.
How to update that particular existing data using django. okay?
So what we will do first is fetch the data which means we will fetch the one particular user from multiple data that we have stored by selecting.
So when you fetch the data here, you will use the get method of django.
Get method is used to fetch particular single data. okay? Right?
So first of all whenever we need to update the data,
We can update it, Only after fetching the data.
We need to select the particular user from The data which is displayed.
That means we have to fetch the data of the user and then update that particular data.
When we update, we will use the save method.
Which method? Save method.
So how to fetch data using it? I will show you practically so that you can understand better. right?
Let's go towards the practical part.
As usual crud application.
We will continue with the same.
Here we have opened the cmd. okay?
First of all let's activate our virtual environment. right?
And open our VS code also. okay?
run the server once.
Let's check how the data looks.
Last time I told you about responsive data.
See in this way our data is displayed.
For example, if I have to update Ram’s data.
So first I need to fetch Ram’s data, right?
All of our data is displayed on the showpage.
Now we want to show two buttons beside this contact, over here.
Which is for edit and delete, because in future videos, we will use the delete button. Clear?
First we will add the buttons.
What you have to do is go back to show.html.
Here two columns are to be added which are for th edit and th delete. okay?
Here if I refresh, two columns should be added, edit and delete. right?
There should be an edit button and a delete button.
What we will do is take a td here and in this td, we will create a form where we will use the post method. right?
And we will give the name to this form as ‘edit form’ okay? Right?
We created a form in django so csrf token is compulsory. right?
I have told you in a previous video at the time of insertion in django.
At that time i told you to use csrf token in every form, if you haven’t watched that video, so first watch that video, understand it properly and then continue this particular video. okay?
so here I want a button.
we will take an input and what we will take as an input type for the button. is ‘submit’
And what is the name of the button, ‘edit’ right?
So what we will write in value, ‘edit’ clear?
Similarly in this way i will copy this td and will create one more td for the delete form.
We will keep the name of this form as ‘delete’.
And this button will work for delete. right?
Now here when i will refresh, so see i got buttons for edit and delete in front of every data
Right? Clear?
What we have to do now is work for editing.
As I will click on edit, that particular data should get fetched and
displayed.
So for that what we will do first is create edit.html here.
Which is already made. okay?
And here we will copy this insert form and paste it on edit.html.
let’s keep the name as ‘update form’ of this form.
Which means we will update here and rub this url, that means we don't want this url now.
And here keep the name of the button as ‘update’. right? Clear?
Now we want to display the data here. okay?
Hope you are able to understand this. right?
When should the data get displayed?
The data should get displayed when the edit button gets clicked.
At that time our edit page should open and the data of that particular user should get displayed.
So how will that happen? Let's see.
First of all we'll create our view. right?
So here I will write ‘edit page view’. okay?
Here i created a view, def edit page, passed a request here.
When should this particular page open?
This particular page should be opened when the edit button is clicked, this page should get displayed with the data
Okay?
So I will take a variable here, ‘get data’ clear?
And now Here we will use the get method to fetch the data.
Which method? Get method. how?
So student.objects.get.
clear?
You need to pass a parameter in this get method which is for id.
Why? Because when we will fetch data of a particular user, we need to fetch through his id.
Here you will pass one more parameter with a request that is pk.
Pk stands for primary key. right?
Here you will write in this way, pk = pk.
Or you can write id=pk. okay?
This id is the name of your field and this pk comes from the function of the parameter. okay? Right?
As you will click on a particular user, id.
For example, I want data of the first user, so I will click on edit. This id will get fetched and through this we will get the data. okay?
Then we will return the render.
Where? Request on edit.html So edit.html.
And now this gets the data, data we got in variable has to be taken to this particular html.
So we will pass our context which is key 2 and here pass a variable through get data. okay?
So we created a new view. right?
So this is the new view, I will write a comment for you over here.
‘ fetching the data of a particular id’ okay?
So we created a new view hence we will create a url of this.
Lets go here and here path edit page slash.
What you will do here is, pass int colon pk, why?
Because we want to fetch the data of that particular person so we need to pass the id inside the view. So the id which we will pass inside the view will be taken from the url okay?
Here call the view of views.edit page.
And here write the edit page okay? Right? Clear?
And the name of this edit page, the name of the url that we created.
We will copy that name, and go on show.html and here in the action, you will Pass it in this way.
It is also a ginja technique, so here you will write a url and here in url we have passed the primary key so pk =? In which way our id is fetching? i.id.
So we will copy it, and paste it here.
we will get this particular id dynamically. right? clear?
So when you click on the edit button of a particular id, it will reach there with the particular id.
Let's check it once, here is our server running, let's refresh it here.
we have made a mistake here .. Okay !
We don't have to write our primary key in this way, remove double curly brackets and write only this.
We get our data as we refresh it.
Now see, as i click edit, see here in url we get one id and our update form is opened.
yet the data is not displayed. right?
See we reached here with the data but did not display the data and see we even got the id here, one, can you see this id? Right? Edit page/1
Now in this way, I will click on 2 to get 2.
I will click on 4 to get 4 right?
That means we got the particular id but we have not fetched the data through that id.
So how will we do it?
So how will we do it?
You have to go here on edit.html, first you have to write the condition above the form to check.
‘If’ now through which key we are sending data, for that have a look in view.
We sent the data on this particular page with the help of key 2 right?
So here we will pass ‘if key 2’ right? Clear?
And this ‘if’ condition that we have opened needs to be closed too.
understood?
Now do I need to run for loop? Do I have to use for loop? NO.
Why not? Because here we are fetching data of a single user right?
So django gives you the facility to fetch data through the key. How? Let's see.
You need to write ‘value’ here and in double inverted commas write ‘key2.first name’.
That is the name of your field okay?
So in this way you will pass values in every input .
And here write the name of a particular field.
In this way here, we will fetch email as well.
Email..
And here contact okay? Right?
Now lets see if our data is displaying.
see still it is blank, let's go back and refresh it..
As i click now, see i got the data for first id.
I will click on the id of the fourth data.
So I got the data of that particular user.
we fetch in django In this way, okay? Right?
I hope you have understood okay?
What we will do now after the data is fetched. Now I want to update this data.
So to update, we will create our new view right? Clear?
we have to update and create a new view.
So lets create a view here right? Clear?
I will write here in the comment ‘update data view’ okay?
And here i created a view ‘update data’ which is a request and here also we will pass the primary key.
Why? Because here we want to update data of particular users okay?
So first of all here we will get the data. right?
I need to get data of a particular user, through the id of the user of which the data has to be updated right?
What we will do here is take the variable ‘udata’ right? Clear?
Same pass query, for objects.get right?
Now the data of that particular user has been transferred into udata okay?
Suppose udata.firstname.
we need to change the first name of that user.
So how will we do it? request.post and here fname.
Now this firstname is column name which means name of the field of the database.
And from where is this fname coming from?
From edit.html, this fname given as Name of the input okay? Right?
So similarly we will fetch all the data which is udata.last name=request.post Lname.
Similarly udata.email = request.post and email.
And our contact is udata. Contact request. Post contact okay? Clear?
So we will get all the data which means this particular data will be available and will get stored in these particular fields.
And now query for update right?
Now we want to fire a query for an update so how will we do it?
Where is all our data stored? Udata right?
So udata.save method.
We will use the save method.
I have told you that to update data we have a save method okay?
So here we used the save method and what page will we open after updating?
Our show page, which page ? show page.
Which means update, data is updated but needs to get displayed.
So the page which is displaying? What is that page? Show page.
How will it get displayed?
The way we did redirect to showpage, write in insert view, similarly here render to showpage okay?
So here we just need to write a redirect to show the page right?
As it will update, it will get displayed on our show page right?
Let's make sure by updating, let's go back and refresh once.
Suppose I will edit the first id and add 123 in email id and update.
So right now nothing happened, why?
Because here we have created a view, so what we had to create instead of a view.
Url ! so path, update here, then we have passed a primary key here as well.
So int colon pk then ‘views.Update data’.
name as update only clear?
Now the url that we made, copy the name of the url.
Go to edit.html and here in action of update form, pass the url in this way.
Which is like this pk= ? here data is coming with the help of key 2.
So key 2.id.
Pass in this manner.
Now let's come back again here, refresh and I will click on edit here.
Pass 123 and then update.
So see our email has been updated right?
So in this way updating and editing works in this particular process.
In this crud operation okay? Right? Clear?
Have you understood in what way the work is happening and how it is done?
Okay?
So when we meet in the next video, we will delete data using 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.
Share a personalized message with your friends.