How to get user IP address in Django

In this Python Django tutorial, I will explain how to get user IP address in Django in simple steps.

I’ve recently developed a project using the Django web framework in which I have a requirement for the user’s IP address. So, here we will cover the steps that we can follow to get user IP address in Django.

Here we will see:

  • IP Address and its use
  • Set up Django Project
  • Get user IP address in Django
  • Return HttpResponse in Django

At the end of this article, you can also download the code to get the user IP address in Django.

How to get user IP address in Django

Now, let us first understand what an IP address is and learn step by step to get the user’s IP address in Django.

IP Address

IP Address stands for “Internet Protocol Address”

In general, an IP address is a number that is allocated to a network device and is used for that device’s identification and location. Basically, IP address is used by computers to connect with one another on different networks and the internet.

And all network device types receive an IP address. It might be a website, camera, laptop, desktop device, or even toys for kids that can connect to the internet.

Read: Python Django format date

Get user IP address in Django

Now, we will see an example related to getting a user IP address in Django.

Set up Django Project

Open the terminal and type the following command to begin a Django project. The Django Project in this example is called UserIpProject.

django-admin startproject UserIpProject

It will make a folder named UserIpProject and type the below command in the terminal to access the project.

cd UserIpProject

By entering the following command in the terminal, we may create a Django app called MyApp inside of this project folder.

python manage.py startapp MyApp
get the user IP address in Django
Set up Project in Django

Add the app name to the INSTALLED_APP list located in the settings.py file, to activate this app.

get visitor IP address in django
Settings.py

A urls.py file is automatically included by Django in the project directory to map the newly created app inside of it. Add the code below to it to do this.

from django.contrib import admin
from django.urls import path,include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('',include('MyApp.urls')),
]

Read: Python Django set timezone

View to get user IP address in Django

To define the main logic for the application, open the views.py file and add the code given below.

from django.shortcuts import HttpResponse

# Create Viewe

def ipaddress(request):
    user_ip = request.META.get('HTTP_X_FORWARDED_FOR')
    if user_ip:
        ip = user_ip.split(',')[0]
    else:
        ip = request.META.get('REMOTE_ADDR')
    
    return HttpResponse("Welcome User!<br>You are visiting from: {}".format(ip))

Create a view named ipaddress that will get the IP address of the user. Then, to get the client’s IP address, use the HTTP_X_FORWARDED_FOR variable.

It stores the IP address when the user is going through a proxy or load balancer. To get the local client’s IP address, it will use the REMOTE_ADDR and it returns 127.0.0.1.

Now, in order to call the view, we must map the view to the URL, thus we must make a file named urls.py in the app directory. Include the following code in it.

from django.urls import path
from . import views

urlpatterns = [
path('', views.ipaddress, name='ipaddress'),
]

Read: Python Django filter

Execute Django Application

To launch a development server type the below-given command in the terminal.

python manage.py runserver

It will open, the page having the user’s IP address.

get visitor IP address in Django
User’s IP Address

Great!!!! We can now see that we are using localhost.

This is how we can get the IP address of the user in Django.

Download complete code of Python Django get IP address

Here is the code:

Conclusion

With this, we have successfully created a Django Project for getting the user’s IP address. We have also learned the meaning of IP address and its use.

Additionally, we have also covered the following topics.

  • IP Address and its use
  • Set up Django Project
  • Get user IP address in Django
  • Return HttpResponse in Django

You may also like to read the following Python Django tutorials.