Skip to content

Instantly share code, notes, and snippets.

@mansha99
Created July 9, 2023 09:08
Show Gist options
  • Save mansha99/f10c42603be99bc31b2d9a1b480a092d to your computer and use it in GitHub Desktop.
Save mansha99/f10c42603be99bc31b2d9a1b480a092d to your computer and use it in GitHub Desktop.
project_app_views.py
from django.http import HttpResponse
from helloapp import signals
from django.utils import formats
from rest_framework.decorators import api_view
from django.http import JsonResponse
import datetime
def hello_world(request):
message = "Hello World"
date_time = datetime.datetime.now()
date_time = formats.date_format(date_time, "SHORT_DATETIME_FORMAT")
ip = get_client_ip(request=request)
device=request.META['HTTP_USER_AGENT']
signals.user_visit_signal.send(sender=request ,date_time=date_time,ip=ip,device=device)
return HttpResponse(message)
def get_client_ip(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')
return ip
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment