# 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
1. Install Django Restricted Countries from PyPI by using ``pip``::
pip install 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**:
```bash
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
MIDDLEWARE = [
...
'restricted_countries.middleware.RestricedCountriesMiddleware',
...
]
DJANGO_RESTRICTED_COUNTRIES = {
"COUNTRIES": ['CN', 'RU'],
"FORBIDDEN_MSG": "Access is denied from your country."
}
Raw data
{
"_id": null,
"home_page": "https://github.com/Mbulelo-Peyi/restricted_countries",
"name": "django-restricted-countries",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.6",
"maintainer_email": null,
"keywords": "django, restricted access, web, country, logging",
"author": "Mbulelo Phillip Peyi",
"author_email": "your.email@example.com",
"download_url": "https://files.pythonhosted.org/packages/1f/ac/e1d9713b5a52c62c6bd8d0f417c23cd961ab3b6d699d611cbc84535531fa/django_restricted_countries-0.1.0.tar.gz",
"platform": null,
"description": "# Django Restricted Countries Middleware\r\n\r\nThis Django middleware restricts access to your application based on the geographical location of the user\u00e2\u20ac\u2122s IP address. Using **GeoIP2** and **ipware**, this middleware can block users from specific countries and return a customizable forbidden message.\r\n\r\n---\r\n\r\n## Features\r\n\r\n- **Country-Based Restrictions**: Blocks users based on their IP geolocation.\r\n- **Customizable Settings**: Block specific countries and customize the forbidden message.\r\n- **GeoIP2 Integration**: Uses **GeoIP2** for geolocation.\r\n- **IP Address Retrieval**: Handles IP address detection, including cases with proxies using **ipware**.\r\n\r\n---\r\n\r\n## Requirements\r\n============\r\n\r\n- Django 3.1 or later.\r\n- `Django IPWare`_ 2.1.0 or later.\r\n- `GeoIP2`_ 2.9.0 or later.\r\n- `MaxMind GeoLite2 country datasets`_.\r\n\r\n## Installation\r\n\r\n1. Install Django Restricted Countries from PyPI by using ``pip``::\r\n\r\n pip install restricted_countries\r\n\r\n### 1. Install Dependencies\r\n\r\nThis middleware requires **GeoIP2** for IP geolocation and **ipware** for retrieving the client\u00e2\u20ac\u2122s IP address.\r\n\r\n#### **Install GeoIP2**:\r\n\r\nTo install **GeoIP2**:\r\n\r\n```bash\r\npip install geoip2\r\n\r\n# Setting up GeoIP2 City and Country Database\r\n\r\nThis 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.\r\n\r\n---\r\n\r\n## Prerequisites\r\n\r\nBefore setting up the **GeoIP2 database**, ensure that:\r\n\r\n- You have a Django project set up.\r\n- You have **GeoIP2** installed. If not, install it via pip:\r\n \r\n- You have a MaxMind account. You can sign up here: [text](https://www.maxmind.com/en/geoip-databases)\r\n- After downloading the files\r\n- tar -xvzf GeoLite2-Country.tar.gz -C ./geoip\r\n- tar -xvzf GeoLite2-City.tar.gz -C ./geoip\r\n\r\n\r\n- You have to add that to your settings file assuming you have the db files in a folder named geoip in the root folder\r\n\r\nimport os\r\n\r\nGEOIP_PATH = os.path.join(BASE_DIR, 'geoip')\r\n\r\n2.Install the MaxMind\u00c2\u00ae GeoIP2 datasets. You can do this in two ways:\r\n\r\n2.1. By running the provided management command for this::\r\n\r\n python manage.py install_geoip_dataset\r\n\r\n\r\n2.2. Or manually, by following the instructions in `GeoIP2 Django documentation`_.\r\n\r\nAfter following those steps, you should be ready to go.\r\n\r\n\r\n3.Install ipware:\r\n- To install ipware which helps retrieve the user's real IP, especially when they are behind a proxy:\r\n\r\n``pip install ipware\r\n\r\n\r\n\r\n#### **Usage**:\r\nsettings.py\r\n\r\nMIDDLEWARE = [\r\n ...\r\n 'restricted_countries.middleware.RestricedCountriesMiddleware',\r\n ...\r\n]\r\n\r\nDJANGO_RESTRICTED_COUNTRIES = {\r\n \"COUNTRIES\": ['CN', 'RU'],\r\n \"FORBIDDEN_MSG\": \"Access is denied from your country.\"\r\n}\r\n",
"bugtrack_url": null,
"license": null,
"summary": "Django middleware to restrict access based on the client's country",
"version": "0.1.0",
"project_urls": {
"Homepage": "https://github.com/Mbulelo-Peyi/restricted_countries"
},
"split_keywords": [
"django",
" restricted access",
" web",
" country",
" logging"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "1ccb6cf77f865c95560f8ef4792b789ed1e56f7a33bc16e4fb6cc45a6ca31466",
"md5": "810baa2b5e46f047b8dc0c0ac7393028",
"sha256": "9d0aafb3506a925b173bea300c6801cdcac9d18b8e09553d1bb194ff434144be"
},
"downloads": -1,
"filename": "django_restricted_countries-0.1.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "810baa2b5e46f047b8dc0c0ac7393028",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.6",
"size": 6154,
"upload_time": "2025-01-10T21:00:26",
"upload_time_iso_8601": "2025-01-10T21:00:26.971051Z",
"url": "https://files.pythonhosted.org/packages/1c/cb/6cf77f865c95560f8ef4792b789ed1e56f7a33bc16e4fb6cc45a6ca31466/django_restricted_countries-0.1.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "1face1d9713b5a52c62c6bd8d0f417c23cd961ab3b6d699d611cbc84535531fa",
"md5": "ba21711ed68b71d10efb1d2cb0bf2bc1",
"sha256": "aaf4210edbc6d7ff27a2cc50c47a91ee9e8c234a572838e6c5c2d0efe5d93016"
},
"downloads": -1,
"filename": "django_restricted_countries-0.1.0.tar.gz",
"has_sig": false,
"md5_digest": "ba21711ed68b71d10efb1d2cb0bf2bc1",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.6",
"size": 5931,
"upload_time": "2025-01-10T21:00:30",
"upload_time_iso_8601": "2025-01-10T21:00:30.700249Z",
"url": "https://files.pythonhosted.org/packages/1f/ac/e1d9713b5a52c62c6bd8d0f417c23cd961ab3b6d699d611cbc84535531fa/django_restricted_countries-0.1.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-01-10 21:00:30",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "Mbulelo-Peyi",
"github_project": "restricted_countries",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "django-restricted-countries"
}