# 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 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**:
```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
INSTALLED_APPS = [
...
'restricted_countries',
...
]
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/6b/15/7292c2fa3e956b5e99518348e93bb98dc4a690c28b744cb54c6df5f73fea/django_restricted_countries-0.1.1.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 django-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\nINSTALLED_APPS = [\r\n ...\r\n 'restricted_countries',\r\n ...\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.1",
"project_urls": {
"Homepage": "https://github.com/Mbulelo-Peyi/restricted_countries"
},
"split_keywords": [
"django",
" restricted access",
" web",
" country",
" logging"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "b92f06ee1ad632ad736533b2f50dbffc6de9e1597b587a87072cd24c787dc494",
"md5": "74afc0b241979b08ee1b809c18e9aa4f",
"sha256": "46b1721be67a2b647a7bbc2067a05d1cdbb674ac0800b0fd90dbd492a6f60705"
},
"downloads": -1,
"filename": "django_restricted_countries-0.1.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "74afc0b241979b08ee1b809c18e9aa4f",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.6",
"size": 6177,
"upload_time": "2025-01-27T01:46:04",
"upload_time_iso_8601": "2025-01-27T01:46:04.520750Z",
"url": "https://files.pythonhosted.org/packages/b9/2f/06ee1ad632ad736533b2f50dbffc6de9e1597b587a87072cd24c787dc494/django_restricted_countries-0.1.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "6b157292c2fa3e956b5e99518348e93bb98dc4a690c28b744cb54c6df5f73fea",
"md5": "c20415aace117ceebb95f479924be149",
"sha256": "e824ffa6dd1fc4320f408b56d743b611f83c01416cf723ecb75ccf10eadb3dc3"
},
"downloads": -1,
"filename": "django_restricted_countries-0.1.1.tar.gz",
"has_sig": false,
"md5_digest": "c20415aace117ceebb95f479924be149",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.6",
"size": 5955,
"upload_time": "2025-01-27T01:46:06",
"upload_time_iso_8601": "2025-01-27T01:46:06.745683Z",
"url": "https://files.pythonhosted.org/packages/6b/15/7292c2fa3e956b5e99518348e93bb98dc4a690c28b744cb54c6df5f73fea/django_restricted_countries-0.1.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-01-27 01:46:06",
"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"
}