How to Get Visitor Detail Information in Django?

ArRosid
3 min readOct 10, 2019

--

In the previous story, I show you how to get visitor IP Address on Django Project. Now, in this story, I will show you how to get visitor detail information like device type that used by visitor, browser type & version, the OS used by visitor, and others.

We will use the project from previous story, if you haven’t read it, you can read it first here

Okay now let’s just start. You can use the project from previous story, or you can just clone my project from github https://github.com/ArRosid/django-visitor-ip

The structure directory of the project will be like this

Above you can see that the project name is django_visitor_ip, and we have one django application named app.

we need to install some packages for this purpose

pip install pyyaml ua-parser user-agents
pip install django-user-agents

After installing that package, we need to edit settings.py file like this

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app',
'django_user_agents'
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django_user_agents.middleware.UserAgentMiddleware'
]

Now let’s edit views.py inside app directory

from django.shortcuts import render, HttpResponsedef home(request):
x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
if x_forwarded_for:
ip = x_forwarded_for.split(',')[0]
else:
ip = request.META.get('REMOTE_ADDR')

device_type = ""
browser_type = ""
browser_version = ""
os_type = ""
os_version = ""
if request.user_agent.is_mobile:
device_type = "Mobile"
if request.user_agent.is_tablet:
device_type = "Tablet"
if request.user_agent.is_pc:
device_type = "PC"

browser_type = request.user_agent.browser.family
browser_version = request.user_agent.browser.version_string
os_type = request.user_agent.os.family
os_version = request.user_agent.os.version_string
context = {
"ip": ip,
"device_type": device_type,
"browser_type": browser_type,
"browser_version": browser_version,
"os_type":os_type,
"os_version":os_version
}
return render(request, "home.html", context)

Above, you can see that we use render, so we need to configure template dirs in settings.py

TEMPLATES = [
{
'BACKEND':'django.template.backends.django.DjangoTemplates',
'DIRS': ["templates"],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]

Next, create templates dir and home.html file like this

Edit home.html like this

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<h1>Welcome Home!</h1>
<h3>You are visiting from :</h3>
<ul>
<li>IP Address : {{ip}}</li>
<li>Device: {{ device_type}}</li>
<li>Browser: {{browser_type}} {{browser_version}}</li>
<li>OS: {{os_type}} {{os_version}}</li>
</ul>
</body>
</html>

Let’s run our django and try to access it

Ahmads-MacBook-Air:$ python manage.py runserver 0.0.0.0:8000
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).You have 17 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
October 10, 2019 - 23:07:39
Django version 2.2.6, using settings 'django_visitor_ip.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.

This is what we will get

Great!!! now we have visitor detail information. This is what I get when try to access the django from my phone

Very cool!!!!!

You can clone this project from my github, https://github.com/ArRosid/django-visitor-ip

I think this is the end of this story, Tomorrow I will cover another very interesting topic. Stay tune!!

--

--