Skip to main content

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

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_restricted_countries-0.1.2.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_restricted_countries-0.1.2-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file django_restricted_countries-0.1.2.tar.gz.

File metadata

File hashes

Hashes for django_restricted_countries-0.1.2.tar.gz
Algorithm Hash digest
SHA256 b1c98b9d5dab05002f26c669a11fda4112f0c0406643195d09ef4eb896e1efd2
MD5 3f050d03ae7e3565ae86e2030e49a526
BLAKE2b-256 c6176e9524a37d7e6ebeb22c7dfc65c83ec77b1bf53ca5d93f36b1a568be1464

See more details on using hashes here.

File details

Details for the file django_restricted_countries-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for django_restricted_countries-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 f38df348f4c3d0d32c51fe9fbdf2800819910d847ffdf749dee68a0d71a42d5b
MD5 1d2915ecdd067baae56f568c9a25a564
BLAKE2b-256 02c3040c063a01e6545fa8ad2e65a98a150080250c20de8794c400bec8d3149c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page