HTTP Shortcuts
Smarter http responses. these are wrappers around the Django HttpResponse class, with a custom error_message attribute for the custom templates, and default error messages.
- class smarter.lib.django.http.shortcuts.SmarterHttpErrorResponse(request, error_message=None, status_code=200, template_file='200.html', *args, **kwargs)[source]
Bases:
SmarterHttpResponse
- class smarter.lib.django.http.shortcuts.SmarterHttpResponse(request, error_message=None, status_code=200, template_file='200.html', *args, **kwargs)[source]
Bases:
HttpResponseA generic HTTP response class for Smarter applications, extending Django’s
HttpResponse. This class is designed to render a specified template with a customizable error message and HTTP status code. It is intended to standardize error handling and response rendering across Smarter Django views.- Parameters:
request (
HttpRequest) – The DjangoHttpRequestobject associated with the response.error_message (
Optional[str]) – An optional custom error message to display in the rendered template. If not provided, a default message is used.status_code (
int) – The HTTP status code for the response. Defaults to 200 (OK).template_file (
str) – The name of the Django template file to render. Defaults to"200.html".args – Additional positional arguments passed to the parent
HttpResponse.kwargs – Additional keyword arguments passed to the parent
HttpResponse. Thecontent_typeis set to"text/html"by default.
- Context:
The template is rendered with a context dictionary containing a single key: -
message: The error message to display.
Example Usage:
from smarter.smarter.lib.django.http.shortcuts import SmarterHttpResponse def my_view(request): # Custom error message and template return SmarterHttpResponse( request, error_message="Custom error occurred.", status_code=400, template_file="400.html" )
- Notes:
This class is intended for use in Django views where a standardized error or informational response is required.
The
template_fileshould exist in your Django templates directory and be designed to display themessagecontext variable.
- Warning:
If the specified template file does not exist, Django will raise a
TemplateDoesNotExistexception.The default error message is generic; always provide a specific message for better user experience.
- class smarter.lib.django.http.shortcuts.SmarterHttpResponseBadRequest(request, error_message=None, *args, **kwargs)[source]
Bases:
SmarterHttpErrorResponseSpecialized HTTP 400 (Bad Request) response for Smarter Django applications. This class extends
SmarterHttpErrorResponseto provide a standardized way to return a “Bad Request” response, rendering the400.htmltemplate with a customizable error message.- Parameters:
request (
HttpRequest) – The DjangoHttpRequestobject associated with the response.error_message (
Optional[str]) – An optional custom error message to display in the rendered template. If not provided, a default message is used: “Dohhhh, that’s a bad request my friend.”args – Additional positional arguments passed to the parent
SmarterHttpErrorResponse.kwargs – Additional keyword arguments passed to the parent
SmarterHttpErrorResponse.
- Context:
The template is rendered with a context dictionary containing: -
message: The error message to display.
Example Usage:
from smarter.smarter.lib.django.http.shortcuts import SmarterHttpResponseBadRequest def my_view(request): if not request.GET.get("important_param"): return SmarterHttpResponseBadRequest( request, error_message="Missing required parameter: important_param" ) # ... rest of the view logic ...
- Notes:
The
400.htmltemplate should exist in your Django templates directory and be designed to display themessagecontext variable.This class is intended for use in views where you want to provide a user-friendly error page for bad requests.
- Warning:
If the specified template file does not exist, Django will raise a
TemplateDoesNotExistexception.Always provide a clear error message to help users understand what went wrong.
- class smarter.lib.django.http.shortcuts.SmarterHttpResponseForbidden(request, error_message=None, *args, **kwargs)[source]
Bases:
SmarterHttpErrorResponseSpecialized HTTP 403 (Forbidden) response for Smarter Django applications. This class extends
SmarterHttpErrorResponseto provide a standardized way to return a “Forbidden” response, rendering the403.htmltemplate with a customizable error message.- Parameters:
request (
HttpRequest) – The DjangoHttpRequestobject associated with the response.error_message (
Optional[str]) – An optional custom error message to display in the rendered template. If not provided, a default message is used: “Awe shucks, you’re not allowed to do that.”args – Additional positional arguments passed to the parent
SmarterHttpErrorResponse.kwargs – Additional keyword arguments passed to the parent
SmarterHttpErrorResponse.
- Context:
The template is rendered with a context dictionary containing: -
message: The error message to display.
Example Usage:
from smarter.smarter.lib.django.http.shortcuts import SmarterHttpResponseForbidden def my_view(request): if not request.user.has_perm("myapp.can_access"): return SmarterHttpResponseForbidden( request, error_message="You do not have permission to access this resource." ) # ... rest of the view logic ...
- Notes:
The
403.htmltemplate should exist in your Django templates directory and be designed to display themessagecontext variable.This class is intended for use in views where you want to provide a user-friendly error page for forbidden actions.
- Warning:
If the specified template file does not exist, Django will raise a
TemplateDoesNotExistexception.Always provide a clear error message to help users understand why access was denied.
- class smarter.lib.django.http.shortcuts.SmarterHttpResponseNotFound(request, error_message=None, *args, **kwargs)[source]
Bases:
SmarterHttpErrorResponseSpecialized HTTP 404 (Not Found) response for Smarter Django applications. This class extends
SmarterHttpErrorResponseto provide a standardized way to return a “Not Found” response, rendering the404.htmltemplate with a customizable error message.- Parameters:
request (
HttpRequest) – The DjangoHttpRequestobject associated with the response.error_message (
Optional[str]) – An optional custom error message to display in the rendered template. If not provided, a default message is used: “Oh no!!! We couldn’t find that page.”args – Additional positional arguments passed to the parent
SmarterHttpErrorResponse.kwargs – Additional keyword arguments passed to the parent
SmarterHttpErrorResponse.
- Context:
The template is rendered with a context dictionary containing: -
message: The error message to display.
Example Usage:
from smarter.smarter.lib.django.http.shortcuts import SmarterHttpResponseNotFound def my_view(request): obj = get_object_or_404(MyModel, pk=some_id) if not obj: return SmarterHttpResponseNotFound( request, error_message="The requested object does not exist." ) # ... rest of the view logic ...
- Notes:
The
404.htmltemplate should exist in your Django templates directory and be designed to display themessagecontext variable.This class is intended for use in views where you want to provide a user-friendly error page for missing resources.
- Warning:
If the specified template file does not exist, Django will raise a
TemplateDoesNotExistexception.Always provide a clear error message to help users understand what could not be found.
- class smarter.lib.django.http.shortcuts.SmarterHttpResponseServerError(request, error_message=None, *args, **kwargs)[source]
Bases:
SmarterHttpErrorResponseSpecialized HTTP 500 (Internal Server Error) response for Smarter Django applications. This class extends
SmarterHttpErrorResponseto provide a standardized way to return an “Internal Server Error” response, rendering the500.htmltemplate with a customizable error message.- Parameters:
request (
HttpRequest) – The DjangoHttpRequestobject associated with the response.error_message (
Optional[str]) – An optional custom error message to display in the rendered template. If not provided, a default message is used: “Ugh!!! Something went wrong on our end.”args – Additional positional arguments passed to the parent
SmarterHttpErrorResponse.kwargs – Additional keyword arguments passed to the parent
SmarterHttpErrorResponse.
- Context:
The template is rendered with a context dictionary containing: -
message: The error message to display.
Example Usage:
from smarter.smarter.lib.django.http.shortcuts import SmarterHttpResponseServerError def my_view(request): try: # ... some logic that may raise an exception ... pass except Exception as exc: return SmarterHttpResponseServerError( request, error_message=f"An unexpected error occurred: {exc}" )
- Notes:
The
500.htmltemplate should exist in your Django templates directory and be designed to display themessagecontext variable.This class is intended for use in views where you want to provide a user-friendly error page for server errors.
- Warning:
If the specified template file does not exist, Django will raise a
TemplateDoesNotExistexception.Always provide a clear error message to help users understand that a server error has occurred.