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
What we have to do now is work for editing.
As I will click on edit, that particular data should get fetched and
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
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?
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.
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.
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?
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?
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.
Update is a query in Django that changes the values of a model.
Some of the common update queries are:
- Add a field to a model
- Remove a field from a model
- Change the value of an existing field in a model
Data is central to any project in the Django framework. It is the foundation of how you build and run your website or web application.
Data management can be a complicated process. It requires lots of different tools, including database migrations, schema changes, and data cleanup. Django provides a lot of features that make this process easier for developers.
The first step towards updating data in Django is to understand what kind of data you have in your project and what kind of updates you want to make to it. If you are only making small tweaks or changes, then it may be best to simply use the default migration tools provided by Django. However, if there are major changes that require a new migration file with a different name, then this needs to be done either manually or with the help of an automated tool like django-admin migrate.
Django update data is a method of updating data in Django. It uses the database's transaction log to record the changes made to the database.
The pros of using Django update data are that it allows developers to work with multiple versions of their database at one time. This is helpful for development purposes and debugging purposes.
The cons of using this method are that it can cause problems for developers when they need to roll back their changes or revert them.
In order to ensure that your Django updates data in your database without any errors, you should use migrations. Migrations are files that contain all of the changes that need to be made to your database and they are run by Django when needed. This way, you will always have a clean database with no errors or leftover data from previous migrations when you need to make changes.
To update data in a database using Django form, you have to follow a few steps.
1. Create the form
2. Create the model to store the data
3. Create an instance of that model and save it to the database
4. Finally, use this instance to update or delete data in the database
The Django framework uses an object-relational mapper called django-orm (originally called django-model) to map database tables to Python classes, which are then used to create Django models.
This is what we need to edit data in Django:
1) Open your python shell
2) Import your model
3) Create a new instance of your model
4) Edit your instance
5) Save the changes
6) Update the changes
7) Delete your instance
8) Delete all instances of the model
9) Exit python shell
The Django migration update data is a list of migrations that have been run by the Django project. It contains all the changes that have been made to the database schema.
The migration update data is updated as soon as a migration has been run, so it should be considered as a live list of migrations.
Configure your Django settings for local debugging if you're using Django.
If Django is used, make migrations.
Reconfigure your Django settings for live deployment.
Commit your modifications to Git and push them to your preferred remote repository.
Method 1: Get updated data and update a section of the page using an AJAX call. You make a GET or POST request to a Django view, receive a response, and then update some (or all) of the page HTML with the new data.
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