handle_error

privex.helpers.django.handle_error(request: django.http.request.HttpRequest, err: str, rdr: django.http.response.HttpResponseRedirectBase, status=400)[source]

Output an error as either a Django session message + redirect, or a JSON response based on whether the request was for the API readable version (?format=json) or not.

Usage:

>>> from django.shortcuts import redirect
>>> def my_view(request):
...     return handle_error(request, "Invalid password", redirect('/login'), 403)
Parameters
  • request (HttpRequest) – The Django request object from your view

  • err (str) – An error message as a string to display to the user / api call

  • rdr (HttpResponseRedirectBase) – A redirect() for normal browsers to follow after adding the session error.

  • status (int) – The HTTP status code to return if the request is an API call (default: 400 bad request)