Django Nigerians Only is a third application that allows developers to restrict access to their applications to only Nigerian users.
Project description
Django Nigerians Only
Django Nigerians Only is a third-party Django application that restricts access to Django applications to only Nigerian users. It can be extended to other countries with a few simple steps.
Requirements
- Django >= 4.1
- geoip2 >= 4.8.0
Installation
-
Install Nigerian Only using pip:
pip install django-nigerians-only
-
Add
'nigerian_only'
toINSTALLED_APPS
in your Django project'ssettings.py
:INSTALLED_APPS = [ ... 'nigerian_only', ]
-
Add the middleware to
MIDDLEWARE
insettings.py
:MIDDLEWARE = [ ... 'nigerian_only.middleware.NigeriansOnlyMiddleware', ]
-
Whitelist allowed countries by setting the list of allowed countries using ISO Alpha-2 country codes. Find Country Codes Alpha-2 & Alpha-3 here.
WHITELISTED_COUNTRIES = ["NG"] # Example: "NG" for Nigeria
-
Download the GeoIP2 database from MaxMind and set the path in
settings.py
. You can read more on setting up GeoIP for Django here.GEOIP_PATH = "path/to/GeoLite2-Country.mmdb"
Usage
Once the above steps are completed, the middleware will restrict access to users from the whitelisted countries.
-
Example Usage with Middleware: The middleware automatically restricts access for all views.
-
Restrict Access Per View: You can also restrict access on a per-view basis using the
whitelisted_country_only
decorator:from django.http import HttpResponse from nigerian_only.decorators import whitelisted_country_only @whitelisted_country_only def restricted_view(request): return HttpResponse("This is a restricted view to only whitelisted countries.")
Important Notes
-
Access will not be restricted if any of the steps mentioned above is not completed correctly.
-
Development Environment: During development, the default IP address is
127.0.0.1
, which cannot determine the user's country. You need to setWHITELISTED_IPS
insettings.py
to allow access during development:WHITELISTED_IPS = ['127.0.0.1']
-
Testing: To test the middleware, use a VPN to change your location to one of the specified countries or use a valid IP address to determine the user's country.
Example Project
You can check out this app with a basic setup example of the package here: Django Nigeria Only Example.
Contributing
Contributions are welcome and appreciated! Follow these steps to contribute:
- Fork the repository.
- Create a new branch for your feature or bug fix.
- Make changes and write tests to ensure your changes don't break anything.
- Push the changes to your fork.
- Submit a pull request.
License
This project is licensed under the MIT License - see the LICENSE
file for details.
Developed by Afeez Lawal
Contact Me:
- Email: lawalafeez052@gmail.com
- LinkedIn: LinkedIn
- GitHub: GitHub
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
File details
Details for the file django_nigerians_only-1.0.tar.gz
.
File metadata
- Download URL: django_nigerians_only-1.0.tar.gz
- Upload date:
- Size: 6.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2ea59683ab85965c44b3e49f7e248adde85cc9ea91eccca5da74142129072a13 |
|
MD5 | 64054876a25d900cd4da1f568b955587 |
|
BLAKE2b-256 | 11a5f4782a835e525511c4866b8c44599a1d63c5175e9a0d71157573dfb363ff |
File details
Details for the file django_nigerians_only-1.0-py3-none-any.whl
.
File metadata
- Download URL: django_nigerians_only-1.0-py3-none-any.whl
- Upload date:
- Size: 7.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 06f3aae37e23fd83c2695fa7d4fb986436bc1becf1079e37b75839e4a58f8491 |
|
MD5 | 978002a246c19aaa7205a29294db1724 |
|
BLAKE2b-256 | 80e47c80cd382c438cb969709d34b6d07b6044ebcc96ca8cda5eac37ecd645ee |