Skip to content

Add docs for headless user bar and accessibility checker #13215

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 3 commits into
base: main
Choose a base branch
from

Conversation

Comment on lines +91 to +103
class UserbarView(TemplateView):
template_name = Userbar.template_name
http_method_names = ["get"]

def dispatch(self, request, *args, **kwargs):
response = super().dispatch(request, *args, **kwargs)
response["Access-Control-Allow-Origin"] = "https://my.headless.site"
return response

def get_context_data(self, **kwargs):
return Userbar(object=None, position="bottom-right").get_context_data(
super().get_context_data(request=self.request, **kwargs)
)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still not 100% sure whether to suggest:

  • "Dismantling" the Userbar component's template_name and get_context_data() to be used in the view, and let the view do the template rendering.
    • Pros:
      • the view is straightforward
      • the response uses Django's TemplateResponse, which means you can test it with things like self.assertTemplateUsed
    • Cons:
      • we're not really treating the userbar as a proper "template component that knows how to render itself"
      • If you subclass Userbar for some reason, the request can only be accessed in the parent_context of get_context_data(parent_context)
  • Letting the Userbar do the rendering (i.e. via Userbar(...).render_html()) and make the view just use the rendered string as-is.
    • Pros:
      • we are treating the userbar as a proper "template component that knows how to render itself"
      • If you subclass Userbar for some reason, the request can be accessed after initialisation
    • Cons:
      • the view is less idiomatic Django (it needs to just return a response using a pre-rendered template string)
      • needs further refactoring of the Userbar class so it accepts request during init rather than inheriting it from the parent context in get_context_data

When writing this docs, I had the idea that we should probably do the second option. Though after looking into it, while I prefer having the request more explicit via init rather than inherited from the parent context, I wasn't convinced by the part where we need to "construct a Django response with a prerendered string" for the view. I think we should just stick with the current (former) approach.

@laymonage laymonage force-pushed the docs/headless-userbar branch from ca88920 to 489b426 Compare July 11, 2025 15:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: 🔍 Reviewing
Development

Successfully merging this pull request may close these issues.

2 participants