CRUD with Django, PostgreSQL, Bootstrap and Heroku — Part 4: Handling requests
In the previous part, we configured our PostgreSQL tables and connected them to our Django app.
Now, we’ll be handling the CRUD operations from the employee_register/views.py.
On this views file, we’ll be creating three functions: One for reading the list of employees, other for inserting and updating the data and the last one will be for deleting. We’ll divide this three tasks into three functions as following:
from django.shortcuts import render
# Create your views here.def employee_list(request): # Read
returndef employee_form(request): # Insert and update operations
returndef employee_delete(request): # Delete
return
Create employee_register/urls.py
from django.urls import path, include
from . import viewsurlpatterns = [ path('', views.employee_form),
path('list/', views.employee_list) #localhost:p/employee/list]
Update employee_project/urls.py
from django.contrib import admin
from django.urls import path, includeurlpatterns = [ path(‘admin/’, admin.site.urls),
path(‘employee/’, include(‘employee_register.urls’))]
Now we’ll create Django templates in order to build the front-end of the views.
Inside employee_register app directory, we create templates/employee_register folders. We should have all the app templates inside this directory. For this example, we create base.html, employee_form.html and employee_list.html.
Make sure your structure is as follows:
In the next part, we’ll figure out how to build on the templates we just created.