https://github.com/anexia/drf-ip-restrictions
A library that allows IP restrictions for views/endpoints in Django REST framework.
https://github.com/anexia/drf-ip-restrictions
django django-rest-framework drf
Last synced: 5 months ago
JSON representation
A library that allows IP restrictions for views/endpoints in Django REST framework.
- Host: GitHub
- URL: https://github.com/anexia/drf-ip-restrictions
- Owner: anexia
- License: mit
- Created: 2022-06-22T07:05:44.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-11-05T13:19:01.000Z (11 months ago)
- Last Synced: 2025-04-13T21:16:26.290Z (6 months ago)
- Topics: django, django-rest-framework, drf
- Language: Python
- Homepage:
- Size: 17.6 KB
- Stars: 4
- Watchers: 3
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# DRF IP Restrictions
[](https://pypi.org/project/drf-ip-restrictions/)
[](https://github.com/anexia/drf-ip-restrictions/actions/workflows/test.yml)
[](https://codecov.io/gh/anexia/drf-ip-restrictions)A library that allows IP restrictions for views/endpoints in Django REST framework.
## Installation
1. Install using pip:
```sh
pip install drf-ip-restrictions
```2. Add the library to your INSTALLED_APPS list.
```python
INSTALLED_APPS = [
...
'drf_ip_restrictions',
...
]
```4. Override the allowed IP addresses your `settings.py` according to your needs:
```python
# within settings.pyDRF_IP_RESTRICTION_SETTINGS = {
"ALLOWED_IP_LIST": ["127.0.0.1"],
}
```## Usage
Add the AllowedIpList class to any views / endpoints that should only provide access for the
configured IP addresses, e.g. to restrict a view set:```python
# within views.pyclass MyViewSet(viewsets.ModelViewSet):
permission_classes = (AllowedIpList,)
...
```or to restrict only a single action:
```python
# within views.pyclass MyViewSet(viewsets.ModelViewSet):
...
@action(
detail=False,
methods=["get"],
http_method_names=["get"],
authentication_classes=[],
permission_classes=[AllowedIpList], # <-- this is the important part for IP restrictions to work
url_path=r"my-method",
)
def my_method(self, request, *args, **kwargs):
# do stuff and return rest_framework.response.Response in the end
```## Django Compatibility Matrix
If your project uses an older verison of Django or Django Rest Framework, you can choose an older version of this project.
| This Project | Python Version | Django Version | Django Rest Framework |
|--------------|-----------------------------|----------------|------------------------|
| 1.1.* | 3.9, 3.10, 3.11, 3.12. 3.13 | 4.2, 5.0, 5.1 | 3.12, 3.13, 3.14, 3.15 |
| 1.0.* | 3.7, 3.8, 3.9, 3.10 | 3.2, 4.0 | 3.12, 3.13 |