DEV Community

Cover image for Love Calculator in Django
Kritebh Lagan Bibhakar
Kritebh Lagan Bibhakar

Posted on • Updated on

Love Calculator in Django

We all have made some basic project like To Do App or Weather app 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

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
Enter fullscreen mode Exit fullscreen mode

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

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

def home(request):
    return render(request,'core/home.html')
Enter fullscreen mode Exit fullscreen mode

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"

from core import views
path('',views.home,name='home'),
Enter fullscreen mode Exit fullscreen mode

And finally, run the server

python manage.py runserver
Enter fullscreen mode Exit fullscreen mode

we can see the "Hello, world!" text on our homepage.

Get the API key

Visit Love Calculator and get your API key or you can just copy the code snippets.

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)
Enter fullscreen mode Exit fullscreen mode

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

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
Enter fullscreen mode Exit fullscreen mode

Final Touch

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

Here is the Github repo of this app

Top comments (0)