In our previous article, we covered the basics of setting up a Django project and created our Exercise model, which we displayed on the front end as a list. In this article, we’ll dive into performing CRUD operations. For those unfamiliar, CRUD stands for Create, Read, Update, and Delete—essentially the four fundamental actions you can perform on your data.
Now that we have our API set up in the app folder, we’ll simply extend the index view to handle create, update, and delete requests.
Let’s set up a form that allows users to create exercises. We’ll be using HTML templates for this purpose once again. To get started, create a new template called add_exercise.html in the app/templates folder.
Next, in our index.html template, we’ll include the add_exercise.html template using the following method:
{% extends "base.html" %} {% block content %}Exercises
{% include 'add_exercise.html' %} ... {% endblock %}
We’re utilizing the include tag here, which promotes composability across HTML templates, making our code easier to maintain and understand. If you refresh the page in your browser, you should see the form appear on the screen.
In our HTML, we're using the
We’re using the
And in our exercises view, we’ll make the following changes to differentiate between requests.
def index(request): if request.method == 'POST': create = 'create' in request.POST update = 'update' in request.POST if create == True: models.Exercise.create(request) elif update == True: models.Exercise.update(request) return redirect('/') exercises = ( models.Exercise.objects.all().order_by("created_at") ) return render(request, "index.html", context={'exercises': exercises})
We check for the button name and forward the request to the appropriate Exercise method accordingly.
Let's add an update class method to the Exercise model in app/models.py.
def update(request): id = request.POST.get('id') title = request.POST.get('title') date = request.POST.get('date') exercise = Exercise.objects.filter(pk=id).update(title=title, date=date) return exercise
To update a row in the database, we can use the update method available on the Exercise model. However, before updating, we need to ensure that we are updating the correct exercise. To do this, we filter the exercises by the primary key, which is id, and update only that specific exercise.
Similarly, we’ll add a delete button next to each exercise in the exercise.html template.
We’ll set delete as the value of the name attribute, and in views.py, we’ll extend the if...elif statements to handle the delete operation.
def index(request): if request.method == 'POST': create = 'create' in request.POST update = 'update' in request.POST delete = 'delete' in request.POST if create == True: models.Exercise.create(request) elif update == True: models.Exercise.update(request) elif delete == True: models.Exercise.delete(request) return redirect('/') exercises = ( models.Exercise.objects.all().order_by("created_at") ) return render(request, "index.html", context={'exercises': exercises})
And in the Exercise model, we'll add the class method delete.
def delete(request): id = request.POST.get('id') is_deleted = Exercise.objects.filter(pk=id).delete() if is_deleted == 1: return True return False
With this addition, we've successfully implemented CRUD operations in our Python and Django application.
from django.http import HttpResponseRedirect def index(request): ... return redirect('/') # or return HttpResponseRedirect(request.META['HTTP_REFERER'])
In summary, by effectively managing our POST requests and ensuring proper redirection, we can create a seamless user experience while implementing CRUD operations in our Django application.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3