This Python package provides a collection of decorators for Django views to streamline handling of request types and parameters.
You can install the package using pip:
pip install django-validate-decoratorsImport the desired decorators from the package and apply them to your view functions:
from django.http import HttpResponse
from inkar.decorators import pass_only_get, pass_json_body
@pass_only_get
def my_view(request, name: str, age: int):
# Your view logic
# Use name and age here
return HttpResponse('Hello World.')pass_only_get: Ensures that the view only processes GET requests.pass_only_post: Ensures that the view only processes POST requests.pass_only_post_and_get: Ensures that the view only processes POST and GET requests.pass_json_body: Parses and validates JSON request bodies.
You can pass parameters to check types and values of request parameters. The following parameters are available:
check_type=False: Checks parameters type, if set True. (Default: False), it throwsTypeErrorif the type is not matched.check_value=HttpResponse: Checks parameters type if set HttpResponse. It throwsHttpResponseif the value is not matched.check_value=Exception: You can customize your own exception class. It throws your exception if the value is not matched.
Contributions are welcome! For major changes, please open an issue first to discuss what you would like to change.
This project is licensed under the MIT License - see the LICENSE file for details.