hx_requests

BaseHXRequest

Base class for HXRequests. Class to be used for basic GET and POST requests.

FormHXRequest

HXRequests class to be used for forms that helps with some of the boiler plate.

DeleteHXRequest

HXRequest for deleting objects.

HXModal

A generic modal that can be used without needing to create a class that inherits from this one.

HXFormModal

A modal to be used with a form.

BaseHXRequest

class BaseHXRequest

Base class for HXRequests. Class to be used for basic GET and POST requests.

Attributes

namestr

Unique name that needs to be matched in the template tag rendering the HXRequest

hx_object_namestr, optional

Name that the hx_object is passed into the context with

GET_templatestr,list, optional

Template rendered for a GET request. If a list is passed in, all the templates are rendered

POST_templatestr,list, optional

Template rendered for a POST request. If a list is passed in, all the templates are rendered

GET_blockstr,list, optional

Block of the GET_template to be used instead of rendering the whole template If a list is passed in, all the blocks are rendered per the GET_template If a dict is passed in, the keys are the templates and the values are the blocks

POST_blockstr,list, optional

Block of the POST_block to be used instead of rendering the whole template If a list is passed in, all the blocks are rendered per the POST_template If a dict is passed in, the keys are the templates and the values are the blocks

refresh_pagebool

If True the page will refresh after a POST request

redirectstr, optional

URL to redirect to after a POST request

return_emptybool

If True, returns an empty HTTPResponse after a POST request

no_swapbool

If True, does not do a swap

show_messages: bool

If True and there is a message set and settings.HX_REQUESTS_USE_HX_MESSAGES is True then the set message is displayed

get_views_context: bool

If True, the context from the view is added to the context of the HXRequest If False, only the context from the HXRequest is used, potentially improving performance by not needing to call the view’s get_context_data method.

kwargs_as_context: bool

If True, the kwargs are added into the context directly. If False, the kwargs are added into the context as hx_kwargs.

Note: Cannot use blocks with a list of templates

is_post_request()

Property : Returns True if it is a POST request

get_context_data(**kwargs) Dict

Adds the context from the view and additionally adds:

kwargs as hx_kwargs
hx_object as {self.hx_object_name} (default is hx_object)
self as hx_request
get_post_context_data(**kwargs)

Adds extra context to the context data only on POST.

get_hx_object(request)

If an ‘object’ was passed in, deserialize it.

get_headers(**kwargs) Dict

Prepare the headers for the response.

get_triggers(**kwargs) list

Override to set the triggers for the response.

get_response_html(**kwargs) str

Prepare the HTML for the response.

render_templates(templates, blocks, **kwargs) str

Renders the templates and blocks into HTML. If templates is a string and blocks is empty then it renders the template. If templates is a string and blocks is a string then it renders the block from the template. If templates is a list then it renders all the templates. If blocks is a list then it renders all the blocks per the template defined. If blocks is a dict then it renders the blocks per the templates in the dict.

get_response(**kwargs)

Gets the response.

set_synchronous_messages(**kwargs)

Convert the hx_message to a Django synchronous message if the page is going to be refreshd (or redirected). This is done because the asynchrounous ones will not show up if the page is reloaded.

get(request: django.http.HttpRequest, *args, **kwargs) django.http.HttpResponse

Method that all GET requests hit.

post(request: django.http.HttpRequest, *args, **kwargs) django.http.HttpResponse

Method that all POST requests hit.

FormHXRequest

class FormHXRequest

HXRequests class to be used for forms that helps with some of the boiler plate. It’s loosely based on Django’s FormView and UpdateView.

Every FormHXRequest must have a form associated with it. The form is passed into the context and is also accessible within the class as self.form.

Override form_valid to form_invalid to inject custom behavior. By default form_valid saves the form and sets a success message. By default form_invalid sets an error message.

Add kwargs into the form using get_form_kwargs

If there is an hx_object it is passed into the form as the form instance.

Attributes

form_classForm

Class of the form attached to the FormHXRequest

add_form_errors_to_error_messagebool

If True adds the form’s validation errors to the error message on form_invalid

set_initial_from_kwargsbool

If True sets the initial values in the form from the kwargs as long as the key matches a field in the form

get_context_data(**kwargs) Dict

Additionally adds the form into the context.

get(request: django.http.HttpRequest, *args, **kwargs) django.http.HttpResponse

Instantiates the form.

post(request: django.http.HttpRequest, *args, **kwargs) django.http.HttpResponse

If the form is valid sets a success message and calls form_valid. If invalid sets an error message and calls form_invalid.

form_valid(**kwargs) str

Saves the form and returns get_response. Override to add custom behavior.

form_invalid(**kwargs) str

Returns get_response. Override to add custom behavior.

get_response_html(**kwargs)

On POST if the form is invalid instead of returning the POST_tempalte the GET_template is returned (the form now contains the validation errors.)

get_form_kwargs(**kwargs)

Return the keyword arguments for instantiating the form. Override to add more kwargs into the form.

get_initial(**kwargs)

Override to set initial values in the form.

get_success_message(**kwargs) str

Message set when the form is valid. Override to set a custom message.

get_error_message(**kwargs) str

Message set when the form is invalid. Override to set a custom message.

get_form_errors(**kwargs) str

concatenates the form errors into an easily readable string.

DeleteHXRequest

class DeleteHXRequest

HXRequest for deleting objects.

The object passed into a DeleteHXRequest is deleted. Override handle_delete for custom behavior.

post(request: django.http.HttpRequest, *args, **kwargs) django.http.HttpResponse

Sets success message and calls handle_delete

handle_delete(**kwargs) str

Called on POST. Deletes the hx_object. Override to add custom behavior.

get_success_message(**kwargs) str

Message set when the object is deleted. Override to set a custom message.

HXModal

class HXModal

A generic modal that can be used without needing to create a class that inherits from this one. It can be used by passing in title and body into the template tag as kwargs and passing in ‘hx-modal’ as the name.

Attributes

body_templatestr

Template used as the modal body

titlestr

Title of the modal, can be passed in as a kwarg and the kwarg will override this attribute

modal_size_classesstr

Classes to set the size of the modal, can be passed in as a kwarg and the kwarg will override this attribute

get(request: django.http.HttpRequest, *args, **kwargs) django.http.HttpResponse

Regular get method but additionally sets the modal body.

get_context_data(**kwargs) Dict

Adds title and body into the context.

HXFormModal

class HXFormModal

A modal to be used with a form. You need to create an HXRequest class that inherits from this one and set the needed attributes for a FormHXRequest.

If the form is invalid the modal stays open and the form contains the validation errors. If the form is valid the modal will close.

Attributes

close_modal_on_savebool

Close modal when form is valid. Set to False to keep the modal open even after the form saves.

get_triggers(**kwargs) list

Override to set the triggers for the response.

get_headers(**kwargs) Dict

Prepare the headers for the response.

get_response_html(**kwargs)

On POST if the form is invalid instead of returning the POST_template the GET_template is returned (the form now contains the validation errors.)

Overrides the get_response_html method from FormHXRequest to use the body_template instead of the GET_template.