Skip to main content

Django Petra Helpers

Helper functions for handling request data in Django Petra applications.

Available Functions

get_request_data

  • get_request_data(request, key, default_value=None)

Extracts specific data from request based on the request method and content type.

from django_petra.helpers import get_request_data

# In your view:
def my_view(self, request):
# Get a file from multipart/form-data
uploaded_file = get_request_data(request, 'profile_picture')

# Get JSON data
user_name = get_request_data(request, 'username', default_value='anonymous')

# Get query parameter
page = get_request_data(request, 'page', default_value='1')

get_all_request_data

  • get_all_request_data(request)

Combines all request data (query parameters, form data, and files) into a single dictionary.

from django_petra.helpers import get_all_request_data

# In your view:
def my_view(self, request):
# Get all request data
all_data = get_all_request_data(request)

# all_data.get('desired_name')
# file: all_data.get('file_desired_name').name

# Example response:
# {
# 'username': 'john_doe',
# 'profile_picture': <UploadedFile: image.jpg>,
# 'page': '1',
# 'filter': 'active'
# }

get_request_headers

  • get_request_headers(request)

Retrieves all headers from the request object.

from django_petra.helpers import get_request_headers

# In your view:
def my_view(self, request):
# Get all request headers
headers = get_request_headers(request)

# headers.get('authorization')

# Example response:
# {
# 'user-agent': 'Mozilla/5.0...',
# 'content-type': 'application/json',
# 'accept': 'application/json',
# 'authorization': 'Bearer token123...'
# }

Complete Example

Here's a complete example showing how to use all helper functions together:

from django_petra.petra_core import Response, ViewSet, status
from django_petra.helpers import get_request_data, get_all_request_data, get_request_headers

class UserViewSet(ViewSet):
def upload_profile(self, request):
# Get specific pieces of data
profile_picture = get_request_data(request, 'profile_picture')
username = get_request_data(request, 'username')

# Get all request data
all_data = get_all_request_data(request)

# Get request headers
headers = get_request_headers(request)

response = {
'status': 'success',
'data': {
'username': username,
'profile_picture': profile_picture.name if profile_picture else None,
'all_data': all_data,
'headers': headers
}
}

return Response(response, status=status.HTTP_200_OK)

Supported Content Types

The helpers support the following content types:

  • multipart/form-data (for file uploads)
  • application/json
  • application/x-www-form-urlencoded

Error Handling

All helper functions include built-in error handling and will raise a ParseError with a descriptive message if something goes wrong during data extraction.

try:
data = get_request_data(request, 'key')
except ParseError as e:
# Handle the error
print(f"Error: {str(e)}")

Request Methods Support

  • GET: Extracts data from query parameters
  • POST/PUT/PATCH: Extracts data from request body and files
  • Other methods: Falls back to query parameters