Javatpoint Logo
Javatpoint Logo

Django CRUD (Create Read Update Delete) Example

To create a Django application that performs CRUD operations, follow the following steps.

1. Create a Project

Django crud example Create a Project

2. Create an App

Django crud example Create an App

3. Project Structure

Initially, our project looks like this:

Django crud example Project Structure

4. Database Setup

Create a database djangodb in mysql, and configure into the settings.py file of django project. See the example.

// settings.py

5. Create a Model

Put the following code into models.py file.

// models.py

6. Create a ModelForm

// forms.py

7. Create View Functions

// views.py

8. Provide Routing

Provide URL patterns to map with views function.

// urls.py

9. Organize Templates

Create a templates folder inside the employee app and create three (index, edit, show) html files inside the directory. The code for each is given below.

// index.html

// show.html

// edit.html

10. Static Files Handling

Create a folder static/css inside the employee app and put a css inside it. Download the css file here Click Here.

11. Project Structure

Django crudexample Project Structure 1

12. Create Migrations

Create migrations for the created model employee, use the following command.

Django crud example makemigrations

After migrations, execute one more command to reflect the migration into the database. But before it, mention name of app (employee) in INSTALLED_APPS of settings.py file.

// settings.py

Run the command to migrate the migrations.

Django crud example migrate

Now, our application has successfully connected and created tables in database. It creates 10 default tables for handling project (session, authentication etc) and one table of our model that we created.

See list of tables created after migrate command.

tables created after migrate command

Run Server

To run server use the following command.

Django crud example Run Server

Access to the Browser

Access the application by entering localhost:8000/show, it will show all the available employee records.

Initially, there is no record. So, it shows no record message.

Django crud example Access to the Browser

Adding Record

Click on the Add New Record button and fill the details. See the example.

Django Crud Example Adding Record

Filling the details.

Django Crud Example Adding Record 1

Submit the record and see, after submitting it shows the saved record.

Django Crud Example Adding Record saved

This section also allows, update and delete records from the actions column.

After saving couple of records, now we have following records.

Django Crud Example After saving Record

Update Record

Lets update the record of Mohan by clicking on edit button. It will display record of Mohan in edit mode.

Django Crud Example Update Record

Lets, suppose I update mohan to mohan kumar then click on the update button. It updates the record immediately. See the example.

Django Crud Example Update Record 1

Click on update button and it redirects to the following page. See name is updated.

Django Crud Example Update Record 2

Same like, we can delete records too, by clicking the delete link.

Delete Record

Suppose, I want to delete Sohan, it can be done easily by clicking the delete button. See the example.

Django Crud Example Delete Record

After deleting, we left with the following records.

Django Crud Example After Delete Record

Well, we have successfully created a CRUD application using Django.

This complete project can be downloaded here.


Next Topic#




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


B.Tech / MCA