How To Add Form Kwargs
In a Django view, form kwargs are added by overriding the get_form_kwargs method.
Similarly, HxRequest provides a get_form_kwargs method that allows you to pass additional information to the form that is not part of the request.
Adding Form Kwargs
Override get_form_kwargs in your HxRequest to add custom kwargs to the form.
from hx_requests.hx_requests import FormHxRequest
class MyHxRequest(FormHxRequest):
# Set attributes
def get_form_kwargs(self,**kwargs):
kwargs = super().get_form_kwargs(**kwargs)
# Add the user to the form
kwargs['user'] = self.request.user
return kwargs
def get_initial(**kwargs):
initial = super().get_initial(**kwargs)
# Set the initial value of 'created_by' field
initial['created_by'] = self.request.user
return initial
Adding An Instance To A Model Form
If you need to pass an instance to a model form, you can do so directly from the template tag.
When using hx_get or hx_post, pass the instance using the object parameter.
If the form is a model form, the object will be passed as the instance.
<button {% hx_get 'my_hx_request' object=my_instance %}></button>
<button {% hx_post 'my_hx_request' object=my_instance %}></button>
Adding Initial Form Values From The Template
To automatically populate initial values from kwargs passed in the template tag, set set_initial_from_kwargs = True on the HxRequest.
If a kwarg passed into the template tag matches a form field name, it will be set as the initial value for that field.
<button {% hx_get 'my_hx_request' created_by=user %}></button>