如何在数据库和django admin中存储IP地址

时间:2022-09-16 13:30:24

Would like to store IP address of everybody who is coming to the site. What is the best approach to do this. Lets say have model

想存储每个来到网站的人的IP地址。这样做的最佳方法是什么?让我们说有模特

class ip(models.Model):
    pub_date = models.DateTimeField('date published')
    ip_address = models.GenericIPAddressField()

What would be the code in models or in views or somewhere that I would save it in the database also would like to save it with user-agent info similar to this.

什么是模型或视图中的代码或我将其保存在数据库中的某个地方也希望用类似于此的用户代理信息保存它。

如何在数据库和django admin中存储IP地址

4 个解决方案

#1


15  

In views.py:

在views.py中:

views.py:

views.py:

    ....

    x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')

    if x_forwarded_for:
        ipaddress = x_forwarded_for.split(',')[-1].strip()
    else:
        ipaddress = request.META.get('REMOTE_ADDR')
    get_ip= ip() #imported class from model
    get_ip.ip_address= ipaddress
    get_ip.pub_date = datetime.date.today() #import datetime
    get_ip.save()

#2


7  

I have gave the example from @Sahil Kalra using middleware,

我使用中间件给了@Sahil Kalra的例子,

Model:

模型:

class IpAddress(models.Model):
    pub_date = models.DateTimeField('date published')
    ip_address = models.IPAddressField()

Middleware:

中间件:

import datetime

class SaveIpAddressMiddleware(object):
    """
        Save the Ip address if does not exist
    """
    def process_request(self, request):
        x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
        if x_forwarded_for:
            ip = x_forwarded_for.split(',')[-1].strip()
        else:
            ip = request.META.get('REMOTE_ADDR')
        try:
            IpAddress.objects.get(ip_address=ip)
        except IpAddress.DoesNotExist:             #-----Here My Edit
              ip_address = IpAddress(ip_address=ip, pub_date=datetime.datetime.now())
              ip_address.save()
            return None

Save the middleware some place in your project folder and In settings file add this middleware. Here is reference How to set django middleware in settings file

将中间件保存在项目文件夹中的某个位置,并在设置文件中添加此中间件。以下是如何在设置文件中设置django中间件

#3


5  

You can fetch IP Address very easily into your views.py.

您可以非常轻松地将IP地址提取到views.py中。

def get_ip_address(request):
    """ use requestobject to fetch client machine's IP Address """
    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')    ### Real IP address of client Machine
    return ip   


def home(request):
    """ your vies to handle http request """
    ip_address = get_ip_address(request)

#4


3  

As you want to save the user agent irrespective of the URL or View which is being called it doesn't make any sense to write this code in the Views or Models.

由于您要保存用户代理而不管正在调用的URL或视图,因此在视图或模型中编写此代码没有任何意义。

You should write a Middleware that would do the work for you. See more about Django middleware : https://docs.djangoproject.com/en/1.6/topics/http/middleware/

您应该编写一个可以为您完成工作的中间件。查看有关Django中间件的更多信息:https://docs.djangoproject.com/en/1.6/topics/http/middleware/

You want to over-ride the process_request() method of your custom middleware to get the IPaddress and useragent from request object and store it in IP model

您希望覆盖自定义中间件的process_request()方法以从请求对象获取IPaddress和useragent并将其存储在IP模型中

The above link will give you absolute clarity about what to do.

以上链接将为您提供绝对清晰的信息。

#1


15  

In views.py:

在views.py中:

views.py:

views.py:

    ....

    x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')

    if x_forwarded_for:
        ipaddress = x_forwarded_for.split(',')[-1].strip()
    else:
        ipaddress = request.META.get('REMOTE_ADDR')
    get_ip= ip() #imported class from model
    get_ip.ip_address= ipaddress
    get_ip.pub_date = datetime.date.today() #import datetime
    get_ip.save()

#2


7  

I have gave the example from @Sahil Kalra using middleware,

我使用中间件给了@Sahil Kalra的例子,

Model:

模型:

class IpAddress(models.Model):
    pub_date = models.DateTimeField('date published')
    ip_address = models.IPAddressField()

Middleware:

中间件:

import datetime

class SaveIpAddressMiddleware(object):
    """
        Save the Ip address if does not exist
    """
    def process_request(self, request):
        x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
        if x_forwarded_for:
            ip = x_forwarded_for.split(',')[-1].strip()
        else:
            ip = request.META.get('REMOTE_ADDR')
        try:
            IpAddress.objects.get(ip_address=ip)
        except IpAddress.DoesNotExist:             #-----Here My Edit
              ip_address = IpAddress(ip_address=ip, pub_date=datetime.datetime.now())
              ip_address.save()
            return None

Save the middleware some place in your project folder and In settings file add this middleware. Here is reference How to set django middleware in settings file

将中间件保存在项目文件夹中的某个位置,并在设置文件中添加此中间件。以下是如何在设置文件中设置django中间件

#3


5  

You can fetch IP Address very easily into your views.py.

您可以非常轻松地将IP地址提取到views.py中。

def get_ip_address(request):
    """ use requestobject to fetch client machine's IP Address """
    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')    ### Real IP address of client Machine
    return ip   


def home(request):
    """ your vies to handle http request """
    ip_address = get_ip_address(request)

#4


3  

As you want to save the user agent irrespective of the URL or View which is being called it doesn't make any sense to write this code in the Views or Models.

由于您要保存用户代理而不管正在调用的URL或视图,因此在视图或模型中编写此代码没有任何意义。

You should write a Middleware that would do the work for you. See more about Django middleware : https://docs.djangoproject.com/en/1.6/topics/http/middleware/

您应该编写一个可以为您完成工作的中间件。查看有关Django中间件的更多信息:https://docs.djangoproject.com/en/1.6/topics/http/middleware/

You want to over-ride the process_request() method of your custom middleware to get the IPaddress and useragent from request object and store it in IP model

您希望覆盖自定义中间件的process_request()方法以从请求对象获取IPaddress和useragent并将其存储在IP模型中

The above link will give you absolute clarity about what to do.

以上链接将为您提供绝对清晰的信息。