Django middleware to restrict access based on the client's country
Project description
Django Restricted Countries Middleware
This Django middleware restricts access to your application based on the geographical location of the user’s IP address. Using GeoIP2 and ipware, this middleware can block users from specific countries and return a customizable forbidden message.
Features
- Country-Based Restrictions: Blocks users based on their IP geolocation.
- Customizable Settings: Block specific countries and customize the forbidden message.
- GeoIP2 Integration: Uses GeoIP2 for geolocation.
- IP Address Retrieval: Handles IP address detection, including cases with proxies using ipware.
Requirements
============
- Django 3.1 or later.
Django IPWare_ 2.1.0 or later.GeoIP2_ 2.9.0 or later.MaxMind GeoLite2 country datasets_.
Installation
-
Install Django Restricted Countries from PyPI by using
pip::pip install django-restricted-countries
1. Install Dependencies
This middleware requires GeoIP2 for IP geolocation and ipware for retrieving the client’s IP address.
Install GeoIP2:
To install GeoIP2:
pip install geoip2
# Setting up GeoIP2 City and Country Database
This guide explains how to set up the **GeoIP2 City and Country database** in your Django project. MaxMind's **GeoIP2** database provides geolocation information about IP addresses, including country and city details. You can use this database to restrict access based on a user's location, retrieve detailed geolocation information, and enhance your web application.
---
## Prerequisites
Before setting up the **GeoIP2 database**, ensure that:
- You have a Django project set up.
- You have **GeoIP2** installed. If not, install it via pip:
- You have a MaxMind account. You can sign up here: [text](https://www.maxmind.com/en/geoip-databases)
- After downloading the files
- tar -xvzf GeoLite2-Country.tar.gz -C ./geoip
- tar -xvzf GeoLite2-City.tar.gz -C ./geoip
- You have to add that to your settings file assuming you have the db files in a folder named geoip in the root folder
import os
GEOIP_PATH = os.path.join(BASE_DIR, 'geoip')
2.Install the MaxMind® GeoIP2 datasets. You can do this in two ways:
2.1. By running the provided management command for this::
python manage.py install_geoip_dataset
2.2. Or manually, by following the instructions in `GeoIP2 Django documentation`_.
After following those steps, you should be ready to go.
3.Install ipware:
- To install ipware which helps retrieve the user's real IP, especially when they are behind a proxy:
``pip install ipware
#### **Usage**:
settings.py
INSTALLED_APPS = [
...
'restricted_countries',
...
]
MIDDLEWARE = [
...
'restricted_countries.middleware.RestricedCountriesMiddleware',
...
]
DJANGO_RESTRICTED_COUNTRIES = {
"COUNTRIES": ['CN', 'RU'],
"FORBIDDEN_MSG": "Access is denied from your country."
}
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_restricted_countries-0.1.2.tar.gz.
File metadata
- Download URL: django_restricted_countries-0.1.2.tar.gz
- Upload date:
- Size: 6.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.11.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b1c98b9d5dab05002f26c669a11fda4112f0c0406643195d09ef4eb896e1efd2
|
|
| MD5 |
3f050d03ae7e3565ae86e2030e49a526
|
|
| BLAKE2b-256 |
c6176e9524a37d7e6ebeb22c7dfc65c83ec77b1bf53ca5d93f36b1a568be1464
|
File details
Details for the file django_restricted_countries-0.1.2-py3-none-any.whl.
File metadata
- Download URL: django_restricted_countries-0.1.2-py3-none-any.whl
- Upload date:
- Size: 6.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.11.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f38df348f4c3d0d32c51fe9fbdf2800819910d847ffdf749dee68a0d71a42d5b
|
|
| MD5 |
1d2915ecdd067baae56f568c9a25a564
|
|
| BLAKE2b-256 |
02c3040c063a01e6545fa8ad2e65a98a150080250c20de8794c400bec8d3149c
|