网站加载时如何在django项目中检查用户IP?
How to check USER IP in django project when website loads?
我正在 django 网站上工作。我想让这个网站的国家受到限制。所以我想在访问网站时检查用户 ip。 url 是什么或加载了哪个应用程序并不重要。因此,如果用户不是来自特定国家/地区,我想向他们显示他们无法访问该网站的错误消息。
我正在使用 GeoLitCity 数据库来获取用户所在的国家/地区。我知道如何获取用户的 ip。但我不知道如何在加载任何内容之前的初始化步骤中执行此操作。谁能帮我解决这个问题?
您可以创建一个custom middleware classValidateUserCountryMiddleware
,它将检查发出请求的国家/地区是否来自特定国家/地区。如果请求来自有效国家/地区,则 Django 将处理该请求。否则,它将 return 一个 HttpResponseForbidden
响应。
在我们的自定义中间件 class 中,我们将定义一个 process_request()
方法。它应该 return None
或 HttpResponse
对象。
上的文档
If it returns None
, Django will continue processing this request,
executing any other process_request()
middleware, then, process_view()
middleware, and finally, the appropriate view. If it returns an
HttpResponse
object, Django won’t bother calling any other request,
view or exception middleware, or the appropriate view; it’ll apply
response middleware to that HttpResponse
, and return the result.
在这里,如果请求不是来自指定国家/地区,我们将return HttpResponseForbidden
回复。
from django.http import HttpResponseForbidden
class ValidateUserCountryMiddleware(object):
"""
This custom middleware class will check the country from where the request
was made using the IP address. If the country is valid, then Django will process
the request. Otherwise, it will return a 403 forbidden response
"""
def process_request(self, request):
# here write the logic to obtain the country from the IP address.
# Then check if its a valid country
if not valid_country: # check if its a valid country
return HttpResponseForbidden('You cannot access the website from this location.') # dont't process the request and return 403 forbidden response
return # return None in case of a valid request
然后将您的中间件添加到 settings.py
文件中的 MIDDLEWARE_CLASSES
。
MIDDLEWARE_CLASSES = (
...
# add your custom middleware here
'my_project.middlewares.ValidateUserCountryMiddleware',
)
我正在 django 网站上工作。我想让这个网站的国家受到限制。所以我想在访问网站时检查用户 ip。 url 是什么或加载了哪个应用程序并不重要。因此,如果用户不是来自特定国家/地区,我想向他们显示他们无法访问该网站的错误消息。
我正在使用 GeoLitCity 数据库来获取用户所在的国家/地区。我知道如何获取用户的 ip。但我不知道如何在加载任何内容之前的初始化步骤中执行此操作。谁能帮我解决这个问题?
您可以创建一个custom middleware classValidateUserCountryMiddleware
,它将检查发出请求的国家/地区是否来自特定国家/地区。如果请求来自有效国家/地区,则 Django 将处理该请求。否则,它将 return 一个 HttpResponseForbidden
响应。
在我们的自定义中间件 class 中,我们将定义一个 process_request()
方法。它应该 return None
或 HttpResponse
对象。
If it returns
None
, Django will continue processing this request, executing any otherprocess_request()
middleware, then,process_view()
middleware, and finally, the appropriate view. If it returns anHttpResponse
object, Django won’t bother calling any other request, view or exception middleware, or the appropriate view; it’ll apply response middleware to thatHttpResponse
, and return the result.
在这里,如果请求不是来自指定国家/地区,我们将return HttpResponseForbidden
回复。
from django.http import HttpResponseForbidden
class ValidateUserCountryMiddleware(object):
"""
This custom middleware class will check the country from where the request
was made using the IP address. If the country is valid, then Django will process
the request. Otherwise, it will return a 403 forbidden response
"""
def process_request(self, request):
# here write the logic to obtain the country from the IP address.
# Then check if its a valid country
if not valid_country: # check if its a valid country
return HttpResponseForbidden('You cannot access the website from this location.') # dont't process the request and return 403 forbidden response
return # return None in case of a valid request
然后将您的中间件添加到 settings.py
文件中的 MIDDLEWARE_CLASSES
。
MIDDLEWARE_CLASSES = (
...
# add your custom middleware here
'my_project.middlewares.ValidateUserCountryMiddleware',
)