Skip to main content

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:

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."
}

Metadata

Release files for django-restricted-countries 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-restricted-countries 0.1.2
File Size Uploaded
django_restricted_countries-0.1.2.tar.gz 6.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-restricted-countries 0.1.2
File Interpreter ABI Platform
django_restricted_countries-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 13.5 kB

Release files / django_restricted_countries-0.1.2.tar.gz

Download URL django_restricted_countries-0.1.2.tar.gz
Size 6.8 kB
Tags Source
SHA-256 checksum
How to use checksums
b1c98b9d5dab05002f26c669a11fda4112f0c0406643195d09ef4eb896e1efd2
BLAKE2b-256 checksum
How to use checksums
c6176e9524a37d7e6ebeb22c7dfc65c83ec77b1bf53ca5d93f36b1a568be1464
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.11.4

Release files / django_restricted_countries-0.1.2-py3-none-any.whl

Download URL django_restricted_countries-0.1.2-py3-none-any.whl
Size 6.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f38df348f4c3d0d32c51fe9fbdf2800819910d847ffdf749dee68a0d71a42d5b
BLAKE2b-256 checksum
How to use checksums
02c3040c063a01e6545fa8ad2e65a98a150080250c20de8794c400bec8d3149c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.11.4

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page