How To Add Context To HxRequests
There are times when you need to pass additional context to the template that is being swapped in, beyond the context that is provided by the view. This can be done in multiple ways, depending on your needs.
Adding Context
Overriding the get_context_data method in the HxRequest is the easiest way to add context to the template that is being swapped in.
from hx_requests.hx_requests import BaseHxRequest
class MyHxRequest(BaseHxRequest):
....
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["important_var"] = "I am important"
return context
This context will get passed to both the GET_template and the POST_template.
Adding Context Only On GET
If you only want to add context on the GET_template, you can override get_context_on_GET.
This is useful when fetching the context is an expensive operation and is not needed in the POST_template.
from hx_requests.hx_requests import BaseHxRequest
class MyHxRequest(BaseHxRequest):
....
def get_context_on_GET(self, **kwargs):
context = super().get_context_on_GET(**kwargs)
context["important_var"] = "I am important"
return context
Adding Context Only On POST
Handling context on POST is slightly different because context often needs to be updated based on the form submission.
For example, if a user’s email is updated in a form, the email displayed in the template should also reflect this change.
By default, hx_object is refreshed, so if the user is the hx_object, their updated email will be reflected.
However, there are cases where you need to update additional context as well.
There are 2 ways refresh the context on POST:
Override
get_post_context_datato add context only on POST.
from hx_requests.hx_requests import BaseHxRequest
class MyHxRequest(BaseHxRequest):
....
def get_context_on_POST(self, **kwargs):
context = super().get_context_on_POST(**kwargs)
context["updated_context"] = "This was updated and now needs to be updated in context"
return context
Set
refresh_views_context_on_POSTtoTruein theHxRequest. This will refresh all the context from the view with updated data.
from hx_requests.hx_requests import BaseHxRequest
class MyHxRequest(BaseHxRequest):
....
refresh_views_context_on_POST = True