Building a contact application with Django and Appsmith
Tags: python, django, appsmith
Published: 2022-05-02 16:13:28

A contact application is a database that collects and stores user contact data. The contact app is a widely used application, similar to the contact application on your mobile phone, that stores data such as user contact names, phone numbers, email addresses, etc. Businesses can use this application to manage their customers' information and store personal contact data.

This tutorial primarily focuses on integrating a contact API in Python using the Django Rest Framework. This application will have functionalities that allow users to create, edit, and delete contact data. While this goes on the backend of the application, we will create the interface of our application using Appsmith's pre-built UI widgets.

[Appsmith](https://www.appsmith.com/) is a powerful open-source low-code framework for creating internal tools with features such as pre-built UI widgets that connect to multiple data sources: APIs, databases, GraphQL, etc., with support for OAuth 2.0 and CURL. 

Appsmith allows you to create web application interfaces with little or no code. You don't need to be a developer to create an application dashboard using Appsmith. Using Appsmith's drag-and-drop UI widgets, we will create a dashboard for our application.

## **Prerequisites**

- Basic knowledge of Python.
- Good understanding of [Django](https://docs.djangoproject.com/en/3.2/)
- An [Appsmith account](https://www.appsmith.com/)

## **Project setup: Building a contact book API**

### **Creating a virtual environment**

We will begin by creating a virtual environment for our project. The essence of a virtual environment is to create a separate environment for all installed packages and dependencies.

The command below shows how to create a virtual environment in your terminal:

```python
pip install virtualenv

virtualenv env
```

## **Activate our virtual environment**

For Windows:

```bash
env\Scripts\activate
```

For Mac/Linux:

```bash
source env/bin/activate
```

*Virtual environment activated!*

**Cloning a repository**
Next, we will be cloning an already existing repository built with the Django Rest Framework. This repo contains a contact application with functionalities that allow users to create, edit, and delete contact data.

To clone the repo, run these lines of code in your terminal:

```bash
git clone https://github.com/Anitaachu/contactAPI.git

cd contactAPI
```

## **Dependencies installation**

Once this is completed, we will be installing a couple of dependencies and packages required for this project using the `pip` These packages are already contained in your cloned requirements.txt file.

Run this command to create a requirements.txt file and install the packages:

```bash
pip install -r requirements.txt
```

Next, migrate the data to the database and create a superuser. To this by running the following commands:

```python
python manage.py migrate

python manage.py createsuperuser
```

## **Testing the application**
Next, let's test this application locally, using the following command: `python manage.py runserver`

In your browser hit the endpoint [http://127.0.0.1:8000/](http://127.0.0.1:8000/). You can also access the admin page using this URL [http://127.0.0.1:8000/admin/](http://127.0.0.1:8000/admin/)
![list_contact](https://files.gitbook.com/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FlZkUHx72OLJMbqkEHff2%2Fuploads%2FD7wmLiWdNZgiVTIagJHb%2Flist_contact.PNG?alt=media&token=4048025c-03bb-40e4-9f69-d745b9822a11)
 
![Admin_page](https://lh3.googleusercontent.com/9xTj7BbR9HUS3PXTczi5MiE3dqE8yPG3p7v4Bl-wkJCa9H8Sc2VStaDRRVpv7WX1Dz52_Air8Fsxhz6uyQKR04Cn3_6mjUKnnB84OTtwScp4dYajcg8fy2WM7dKfjl2RX4hiikjG)

Great! Our application is running!🎉

## **Getting started with Appsmith**

With our API up and running, the next phase of this project is creating an interface using Appsmith pre-built widgets and connecting these widgets to our data source (API). We will be doing this by writing some queries that will be implemented with the widgets.

*Let’s dive in!*

### **Connecting to a Datasource**

It takes a few minutes to connect to a datasource, and you can develop tools on the database of your choosing. You can either connect to a database, an API, or both. For this project, we will connect Appsmith to a deployed API using the API we earlier created. The deployed API can be accessed using this URL: [https://contactapp-api.herokuapp.com/](https://contactapp-api.herokuapp.com/)

![appsmith_dashboard](https://lh4.googleusercontent.com/feC3mw4W_fZHjDepzaFiKASntWwK4yQEHvP1umBxYQV4V1nrDNoHdaLNRTBq4iWP_Z4e4orNFAelVhu6dTUO3LnqjMNQfiD3374wV1gEe7eh6Fqx1DUNM4RXvLQxkUiU5ygkv876)

- _Ensure the API you intend to connect is already deployed. You can use Heroku or any other deployment service._

Add the URL of your deployed API and click on ‘*run'*.

![datasource](https://lh5.googleusercontent.com/E43MI5xAw0dmcPTMcSigbgaNhg1jKjC0pHPQOk6qS6VrLjs9O0PcX82qchsZgyjaH0VH9JalWmaOZEQfr44_NL0b4zppXdX9L3ram0bBCNYsStRWOwcEHlvERTC1OS_Kd_MQmMjY)

Datasource successfully connected!

Now let’s get to work!

### **Creating a dashboard with the Appsmith widget**

With Appsmith, you can build a fully customized interface with no code. Appsmith has many pre-built UI widgets such as tables, forms, buttons, boxes, images, and more, all available to help you create exciting applications. 

Let’s begin! Firstly, let's create a table that will display our contact data.

Follow these steps:

- From the widget section, drag and drop the table widget onto the canvas in this manner:

![dashboard](https://user-images.githubusercontent.com/52820835/157263509-ee2bb8e8-7d38-41ab-84e1-520eae5ec0fd.gif)

- Edit the default data from the property pane and add the column names as contained in our database. As contained in our database, the column names include "_first_name", "last_name", "phone_number", "email", "address",_ and _"birthday."_

![dashboard](https://user-images.githubusercontent.com/52820835/157263688-7aac74d5-2ec8-46b0-acbc-263ca585a736.gif)

- Next, edit the Table Data property in Table Widget’s property pane to  `{{Contact_API.data}}` in other access data from the query.

![dasboard](https://lh5.googleusercontent.com/c_q9bN0GzZUGrqc_5Tf1VZF3fcOFcSZfWbGgvEkp9wF4reaKU128181_8iysbAVILYLQ4vW2L2XA2BcXNEW7iXSZmn_aNm6hWTj3hjF_PamLLF37YQix1f6sZ254YujAiFfB-0DQ)

Next, we will create a button widget that will be used to create contacts. When a user clicks this button, it navigates to another page that contains a form widget.

### **Adding a button and form widget**

Drag and drop the button widget into the canvas.

![dashboard](https://user-images.githubusercontent.com/52820835/157264443-8a7bdb0a-0eaa-4644-adb6-34e407de80aa.gif)

In the label section, change the ‘*submit*’ label to '*create contact'.* Next, edit the button widget, in the _*Events > onClick > Navigate to.*_

![dashboard](https://lh4.googleusercontent.com/6jRGXyzCv2dufZ5eTiD1UO8v6DUGHQIawaHQcvqNgXOTHSc5hBpF1-8KodzZdu2LwGlELwk5YwLQTwjB0oJyMWClM_WizhZqEvv_W92o78pCqvFIlO74ncXckgKDuZ53SYeQggeU)

Under "*Page Name* or *URL*" type in '*Page 2*'. By doing this, when we click on the *"**Create contact**"* button, it navigates us to a new page that will take us to the contact form.

Lastly, click on the "+" icon at the top left to add a new page.


### **Form widget**

This new page will contain a contact form that will be used to add a new contact, using input fields where users can enter the contact data they wish to create.

Let’s create a form using a form widget with these few steps:

- From the widget section, drag a form widget into the canvas.

![form_widget](https://lh3.googleusercontent.com/NNbqNoHqrQTLx0OK0CBPvh5-LnAEKC8Hyy3B3V9wbIMZCva7hEWJCHVaxOrrV9m8vJaxm_tPBT1e81Lr0yaL77M7ecnUnm0fwNp-MLOdxXrLEprZLI1zqL0gsTz1h96ayVRh9jB3)

- Change the "*Form*" label to "*Create contact*".
- Next, drag and drop text widgets into the form widget we just created. Edit the label name to first name, last name, etc. In this manner:

![form_widget](https://lh5.googleusercontent.com/I-qcxYC1d_WPcS6fGNfZOd8zd0keG80uvwsSH30_cKfQJRlVSTnhQrqMtsT-djIvAkXTnBpAJMlJNTN2e60DCnJEkTjU2XS45TcZHRFq3o9rOLCI-fwSaRDvpyDspjRclnBfyPW2)

- Add the input widget to the canvas. This widget will allow the user to enter their contact name, phone number, address, etc.

![form_widget](https://lh4.googleusercontent.com/EvKqdYSonDac15TUyGUdMqzLhcgevJh4Vtb4M-3zR9zX3EBMKUPRdE0OAsfNx05w_JKxt9cGYB8L7Pjc76KK4gyS5N-FT9rjwmUMsXv3SkwIpKEb871i8vLa0lMVjsNYDCjRphLQ)

- Add functionality to the submit button. To do this, create a new query on the right side of the page.

![form_widget](https://lh6.googleusercontent.com/jN5Pbo2jfNROou1w6xqsJ08k1DxbbX9sJ_b6iH_E1-UrwMr4f9hBNXmSp42jA9kobdB2OuwCF3LnhHrgo6wxuEg1mKF6F1xe1IBykT-xDDzSWpWqWDaODlHbfsnZ0hbLjTr-YEoM)

- Create a new datasource and enter the URL of the datasource, [https://contactapp-api.herokuapp.com/](https://contactapp-api.herokuapp.com/). You can also change the name of this datasource to be able to identify it.

![form_widget](https://lh5.googleusercontent.com/l275yCm3fX_w-QOIymEn3JzCgIjTHbqfcE2c6tBZsT7kJmsBup9JEq1J8PPrkVmp5GoIYSZXuheNC9aeopPZstczKlCfZoqWBG3mRaidRni1L6yzk3CLw-zWcWsR-Bm7xCflQHN7)

- Add connect the query to the submit button.

![form_widget](https://lh6.googleusercontent.com/lTbmqSM8GC8Crft7ueyZxP0AsyTi8Py6zjy0CNnATFye9DX-Mwg8qrV4AFvL5-mvOSN8Qqho25HA4Tdf6aLvcSy1Thh7AWpvEIRxTyrdA0nwxX95Wiz12jjYzSNamYIxUh2FY96E)

- Lastly, set the body property of your datasource to:

```
{
"first_name": "{{Input1.text}}",
"last_name": "{{Input2.text}}",
"phone_number": "{{Input3.text}}",
"email":"{{Input4.text}}",
"address":"{{Input5.text}}",
"birthday": "{{Input6.text}}"
}
```

By doing this, a connection will be created between the widget and the datasource.

Now let’s test out our widget to ensure that it's functioning. Firstly, on the contact dashboard, click on the "*Create Contact*" button. The button would redirect to another page that contains a contact form.

![widget_5_new](https://user-images.githubusercontent.com/52820835/157268605-ed5e5021-cd4d-4282-b35b-ee8edf191e09.gif)

Fill out this form and hit the submit button. You will notice that contact details that are added through the form are displayed on our application dashboard.

![form_widget](https://lh6.googleusercontent.com/ppQGPQXYzkEB6xEWl6UKayb7sL3fICKHJ4u4_4dWetN-O9grCs9zpMFZd3XLqAalk1zeguCgESIqQ8rjVfia_2RvOK3TuT-qzchqA2D1CUhm8gSVC42pFdicDEMgGyAKT2aYHhYc)

So far, we have been able to create a form widget where users can create contacts, and these contacts will be displayed on our dashboard.

*Great!*

Asides creating contacts, contact applications also have features where users can edit contacts or delete contacts that are no longer needed. Let’s add the edit and delete contact buttons and write queries that will connect these buttons to our datasource.

### **Edit contact button**

In our application dashboard, add an edit button. This button will also be connected to our datasource. Let’s work on our edit button.

- **Step 1**: In the application dashboard, create a new column named "edit contact".
- **Step 2**: Next, click on the Settings symbol in the “*Edit Contact”** column to add some functions to it.
- **Step 3**: Replace the following:
- **Column Type:** Choose ‘_Button_’.
- ***Label***: Replace ‘_Action_’ with ‘_Edit_’.
- **onClick**: Choose ‘_Open modal_’
- Lastly, under 'Modal Name', create a new modal.

When you click on the edit button, it spins up a modal. We will edit this modal and add some queries to it to enhance its functionality. To do this:

- First, create a text and input widget similar to that of the form widget in the modal. This way:

![modal](https://lh3.googleusercontent.com/17tSgFquEOwhR-RGs6L_paZ-Vodvv9mlXHz22IEppdwsnSrKCc9ImsIna-FptiwB7ktULZ8J3Ayc8LUxFqsMewU-xxlL5iZh25X-FV_kBm6XYfV5G0GcSXcKEKpXE_1GyRaNELLF)

- Add a new datasource and enter the URL of the datasource.

![datasource](https://lh6.googleusercontent.com/iO9vgUoqPILFYvYV-zA_C29otarOgbH_MMjfNAhKrXSQLVjGyahJgbrVDg0NmFNAWRCAtVPr9vD0gUBLwRGhzFz8_wLn7h1ky1-lp11CbpR2cpatXZDjhO2n7bWgutjb776VqWV3)

Now you can edit a contact by clicking on the edit button and adding new data to replace the former contact data.

For the delete function, we simply repeat the same process as that of the edit button. However, the modal of the delete may differ. The Delete modal should appear like this:

![modal](https://lh3.googleusercontent.com/QzZteeEujuTdgVqdWt3fXqgz_xv2VPlkdq_EpOWQbaQh0-UwKIaVolMOjt0pShashbBR_vRF4EoCRD6BOd36YQUN1JrZjzRJkcE-n2-xUC4obqIUxQjeXY5P2xAk0skVjWG-l14I)

Just as we did for the first modal, create a query that will connect the delete button widget to a data source.

![button_widget](https://lh3.googleusercontent.com/OE7Q8ZzalTVmW7NM2KArVhmMQcQfsBNB9agj2JN8eSL_FAD0rCBoK2Ck0ooNq0wPM_Rbf7054c1HxVCT0OgctSz6_5YqhodKs4spQnQUrsnovs3VH7DfqdB32kQfC9dZTyRLHPe4)

Set the body property of your datasource to: `{{Table1.selectedRow.id}}`

Lastly, execute a query under _Events > onClick_. This button will query data from the deleted datasource you have created.

![button_widget](https://lh5.googleusercontent.com/npKR9mxd5YppbHUJjoA58ciYPLYMZ5h-xmkiksPbjvjqM4EsiXj2gJ_VsazEpvLhzCaKYdeZhr4VC8hAHQvdGwKYdSMPc5Dzr_eY_bqOz1hxpSmQTcGhgAqN5pTEdN6-nPZiPmuT)

Let’s test out the delete method to see if it is working properly.


![delete_method](https://user-images.githubusercontent.com/52820835/157284656-bb6a605e-fe76-472a-96a6-d71c2347f354.gif)

Nicely done! Our widgets and datasources are well implemented and functioning properly.

## **Deployment**

So far, we have done a great job. The last phase of this project will deploy our application. On the right section of the dashboard, click on _'Deploy'_. You can also deploy this application by connecting to a Git repository.

*Our application is deployed!* 🎉

Finally, to test the work we have done so far. Try creating a new contact:

![deployment](https://user-images.githubusercontent.com/52820835/157284930-d04a6b76-9e4a-462e-a1ca-74428fef9b7d.gif)

We will also test the delete button to ensure it's working as well:

![delete_button](https://user-images.githubusercontent.com/52820835/157285060-ff3bcbb6-eafa-4a16-af80-ec5b660da5bc.gif)

*Successful!*

In this tutorial, we created an amazing user interface for our contact application with low code. I hope you had fun building!

Happy coding!🙂