Django CRUD (Create, Retrieve, Update, Delete) Function Based Views


CRUD is abbreviated as Create, Retrieve, Update and Delete Functions. In Django, we can perform all these CRUD operations on the data we created into the database. Now let’s create the views.py function based on the operation that we want to perform.

Create Operation with Django

Create is used to create or add new data entries to the database table.

In the following snippet, we begin by importing both the model form and the corresponding model. Next, we proceed to check whether the request is a POST request. If it is a POST request, we create a new form instance using the data from the request, validate it and save the data and redirect the user to the homepage.

On the other hand, if the request is not a POST request, we can simply create a new instance of the form and render it within the context.

from django.shortcuts import render, redirect
from .forms import MyModelForm
from .models import MyModel
def create_view(request):
   if request.method == 'POST':
      form = MyModelForm(request.POST)
      if form.is_valid():
         form.save()
         return redirect('appname:home')
   else:
      form = MyModelForm()
   context = {'form': form}
   return render(request, 'appname/create.html', context)

Retrieve Operation with Django

Retrieve is used to retrieve, read, search or view the existing data entries in a group or specific data entry. In the views.py, we import the model and the get_object_or_404 function, which raises a 404 error if the object doesn't exist. We retrieve the object using its primary key, which is passed as an argument to the view. We then render the object in the context.

from django.shortcuts import render, get_object_or_404
from .models import MyModel
def detail_view(request, pk):
   mymodel = get_object_or_404(MyModel, pk=pk)
   context = {'mymodel': mymodel}
   return render(request, ''app_name /detail.html', context)

Update Operation with Django

This is used to edit or update the previously entered data in the database table. In the views.py file, we retrieve the object using its primary key, and then we check if the request is a POST request. If it is, we create a new instance of the form with the data from the request and the instance of the model we retrieved.

If the form is valid, we save it and redirect the user to the detail view of the object. If it's not a POST request, we create a new instance of the form with the instance of the model we retrieved and render it in the context.

from django.shortcuts import render, get_object_or_404, redirect
from .forms import MyModelForm
from .models import MyModel
def update_view(request, pk):
   mymodel = get_object_or_404(MyModel, pk=pk)
   if request.method == 'POST':
      form = MyModelForm(request.POST, instance=mymodel)
      if form.is_valid():
         form.save()
         return redirect(''app_name:detail', pk=pk)
   else:
      form = MyModelForm(instance=mymodel)
   context = {'form': form}
   return render(request, 'app_name/update.html', context)

Delete Operation with Django

Delete is used to delete, remove or deactivate existing entries in the database table. In this example, we import the model form and the model itself. We import the model itself and retrieve the object using its primary key, and then we check if the request is a POST request. If it is, we delete the object and redirect the user to the home page. If it's not a POST request, we just render the object in the context.

from django.shortcuts import render, get_object_or_404, redirect
from .models import MyModel
def delete_view(request, pk):
   mymodel = get_object_or_404(MyModel, pk=pk)
   if request.method == 'POST':
      mymodel.delete()
      return redirect('myapp:home')
   context = {'mymodel': mymodel}
   return render(request, 'myapp/delete.html', context)

Updated on: 06-Nov-2023

143 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements