CRUD with Django, PostgreSQL, Bootstrap and Heroku — Part 4: Handling requests

Carlos
2 min readMay 23, 2020

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
return
def employee_form(request): # Insert and update operations
return
def employee_delete(request): # Delete
return

Create employee_register/urls.py

from django.urls import path, include
from . import views
urlpatterns = [ 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, include
urlpatterns = [ 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.

--

--

Carlos

Software Developer and seldom writer. Creates technology and studies its effects on people and society.