In this Django article we are going to learn about IP Address Lookup Application , so for creating
IP Finder Application we are going to use ipapi library.
Learn Django REST Framework for Beginner in Python
Also you can read more django articles
1: Django Pagination Complete Example
2: Django Sending Email to Gmail Account
3: Build News Application in Django
5: Django User Authentication Framework
Installation
First of all you need to install ipapi library, you can use pip for the installation.
1 |
pip install ipapi |
So after installation you need to create a New Project in Django, you can use this command
for creating of the Project in Django.
1 |
django-admin startproject IPFinder |
After creating of Django Project you need to change directory to the created Project, basically
we are going to create Django App.
1 2 |
cd IPFinder python manage.py startapp IPApp |
First of all you need to create a templates folder in your Project. and add two html files in
the templates folder. the first one is base.html and the second one is index.html, this is the
structure of our Django Project.
Also after creating of the templates folder, you need to tell Django about this folder, so open
your settings.py file, and in the TEMPLATES you need to add your templates name like this.
1 2 3 4 5 |
TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': ['templates'],#templates name added here 'APP_DIRS': True, |
So now open your views.py file in your Django App, in our case the App name is IPApp and
add this code. basically in the Index view function we are going to first get the input from user,
right now we have not created our html code, after that we use our ipapi library for finding the
IP locations and information. also you need to send the information in your index.html.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
from django.shortcuts import render import ipapi # Create your views here. def Index(request): search = request.POST.get('search') data = ipapi.location(ip=search, output='json') context = {"data": data} return render(request, 'index.html', context) |
Now this is our base.html. also we have used Bootstrap CDN linke in here.
templates/base.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous"> <title>{% block title %} {% endblock %}</title> </head> <body> {% block body %} {% endblock %} </body> </html> |
And this is the index.html, basically we have a text input field for searching IP Address, also we
are rendering the data in our index.html file.
templates/index.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
{% extends 'base.html' %} {% block title %} IP Finder {% endblock %} {% block body %} <div class="container"> <br> <br> <h1>Django IP Finder</h1> <br> <br> <form action="{% url 'index' %}" method="post"> {% csrf_token %} <div class="form-group"> <input type="text" class="form-control" name="search" placeholder="Enter IP Address"> </div> <input type="submit" class="btn btn-primary" value="Search"> </form> <br> <br> <p>IP Address is : {{data.ip}}</p> <p>City Location is : {{data.city}}</p> <p>IP Country is : {{data.country}}</p> <p>Available Language is : {{data.languages}}</p> <p>Latitude Information Is : {{data.latitude}}</p> <p>Longitude Information Is : {{data.longitude}}</p> <p>Time Zone Information Is : {{data.timezone}}</p> <p>Availble Calling Code : {{data.country_calling_code}}</p> <p>Available Currency Is : {{data.currency}}</p> <p>Internet Network Is : {{data.org}}</p> <p>Continent Code Is : {{data.continent_code}}</p> <p>Postal Code Is : {{data.postal}}</p> <p>UTC Offset Is : {{data.utc_offset}}</p> </div> {% endblock %} |
Now we need to create our url routing, first you need to create a new python file at name
of urls.py in your Django App, in my case it is IPApp, and add this code.
1 2 3 4 5 6 7 |
from django.urls import path from .views import Index urlpatterns = [ path('', Index, name = 'index'), ] |
And this is our Project urls.py, basically we have included our App urls.py in here using
include method.
1 2 3 4 5 6 7 8 |
from django.contrib import admin from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), path('', include('IPApp.urls')), ] |
So now run your Django Project.
1 |
python manage.py runserver |
Go to http://localhost:8000/ and this will be the result.
Also you can watch the complete video for this article
Subscribe and Get Free Video Courses & Articles in your Email
Thanks for this tutorial. How do i implement IP redirect based on region the website visitors are coming from?