Skip to content

Add self.headers field to InertiaResponse to enable customizability #79

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ToliaGuy
Copy link

@ToliaGuy ToliaGuy commented May 26, 2025

Hello, I am very excited, this is my first contribution to open source software (so please have patience with me)

Reason for this MR is that I have created my custom Middleware to pass django messages through inertia (code below), but the headers is only value in the InertiaResponse that is not saved as an instance field, so I can't recreate from one response another one.

But if you have any better ideas, that would be even better.

EDIT: simplified approach in my code example

class DataShareMiddleware(object):
	def __init__(self, get_response):
		self.get_response = get_response

	def collect_messages(self, request) -> list:
		messages = []
		for msg in get_messages(request):
			message = {
				"message": msg.message,
				"level": msg.level,
				"tags": msg.tags,
				"extra_tags": msg.extra_tags,
				"level_tag": msg.level_tag,
			}
			messages.append(message)
		return messages
	
	def __call__(self, request):
		"""
		Collecting messages before running the view works out of the box
		the issue comes when the view adds messages, since share method
		works with request object, share method doesn't work after the execution
		of the view

		Workaround: collect messages after the view
                run share function		
		and then recreate the response with edited request
		"""
		# collectin message before view works out of the box
                # messages = self.collect_messages(request)
                # share(request, messages=messages)
		response = self.get_response(request)
		# collecting messages after view is problematic, since inertia's share function
		# works with request object
		if hasattr(response, 'request') and hasattr(response.request, 'inertia'):
                       messages = self.collect_messages(request)
                       if messages:
			    share(request, messages=messages)
			    # for now doesn't work with headers, cuz headers are not stored
			    # as property in InertiaResponse
			    # TODO: create MR for inertia-django to add headers to add headers field to the response
                            # so I can access it with response.header
			    response = InertiaResponse(
				request,
				response.component,
				response.props,
				response.template_data
			    )
	        return response

@BrandonShar
Copy link
Collaborator

Thanks for the PR @ToliaGuy !

To make sure I understand, it's the share method that needs the headers? Is that a django method?

@ToliaGuy
Copy link
Author

ToliaGuy commented Jun 4, 2025

Hi no its inertia's function that enables you to share data with components without you having to explicitly pass it in render function.

but this PR is not really relevant anymore, this works fine:

response = self.get_response(request)
        if hasattr(response, 'request') and hasattr(response.request, 'inertia'):
            messages = self.collect_messages(request)
            if messages:
                share(request, messages=messages)
                response = InertiaResponse(
                    request,
                    response.component,
                    response.props,
                    response.template_data,
                    response.headers
                )

basically if you want to share all the flash messages from django (even the ones that were created in the view, you have get the messages, share them with the share function (which will attach them to the request) and then from the old response (without the new shared data) rebuild new InertiaResponse with the shared data.

And the headers field is available from the base django response, so my PR useless and can be closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants