Love Calculator in Django
Tags: django, python
Published: 2021-05-05 15:01:04

We all have made some basic project like [To Do App](https://github.com/kritebh/django-to-do-list) or [Weather app](https://github.com/kritebh/django-weather-project) so let's build something fun and interesting today!

I was just scrolling through the directory of RapidAPI and found an awesome API by Ajith.

![Let's Do it](https://media.giphy.com/media/3IUZqk1EggpftgrBi1/giphy.gif)

Open VS Code or your favourite text editor then terminal and set up a virtual environment for creating a Django Project by following commands

## Setup Venv and Django Project

```
python -m venv env
cd env/Scripts
./activate
cd ../..
pip install django
pip install requests
django-admin startproject lovecalculator
cd lovecalculator
python manage.py startapp core
python manage.py runserver
```
We have set up our Project and created an app and now make sure you have added "core" app in the INSTALLED_APPS of settings.py file.

## Setup the templates

Create a "templates" folder and then "core" folder within the templates folder.

Now create "home.html" and paste the Bootstrap [starter template](https://getbootstrap.com/docs/5.0/getting-started/introduction/)

Create a function for the homepage in "views.py"

``` python
def home(request):
    return render(request,'core/home.html')
```
Later we will add all the logic of our app here.

Before we run the server just create an URL for the homepage in "lovecalculator/urls.py"

```python
from core import views
path('',views.home,name='home'),
```
And finally, run the server 

```python
python manage.py runserver
```
we can see the "Hello, world!" text on our homepage.

## Get the API key 

Visit [Love Calculator](https://rapidapi.com/ajith/api/love-calculator) and get your API key or you can just copy the code snippets.

```python
url = "https://love-calculator.p.rapidapi.com/getPercentage"
querystring = {"fname":"John","sname":"Alice"}
headers = {
     'x-rapidapi-key': "Your RAPID API KEY",
     'x-rapidapi-host': "love-calculator.p.rapidapi.com"}
response = requests.request("GET", url, headers=headers, 
params=querystring).json()
print(response)
```
Paste this code in "home" function.
Hit the homepage again and you will get the response in the terminal.

Now we are getting our desired data, so just arrange it and send it to templates.

In the "views.py" I have created a separate function for fetching the data from API but you can simply add in the "home" function also.
home function
```python
try:
     if request.method=='POST':
        f = request.POST.get('fname',False)
        s = request.POST.get('sname',False)
        res = calculator(f,s)
        context ={
        'fname':res['fname'],
        'sname':res['sname'],
        'percent':int(res['percentage']),
        'result':res['result'],
            }
        return render(request,'core/result.html',context {'data':context})
        return render(request,'core/home.html')
    except:
        return render(request,'core/error.html')

def calculator(f,s):
    url = "https://love-calculator.p.rapidapi.com/getPercentage"

    querystring = {"fname":f"{f}","sname":f"{s}"}

    headers = {
        'x-rapidapi-key': "Your RAPID API Key",
        'x-rapidapi-host': "love-calculator.p.rapidapi.com"}

    response = requests.request("GET", url, headers=headers, params=querystring).json()
    return response
```

## Final Touch

I have added some more HTML file and static files for designing it.

Here is the Github repo of this app
{% github kritebh/django-love-calculator no-readme %}