-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
base: main
Are you sure you want to change the base?
Conversation
ba1c064
to
585b627
Compare
585b627
to
ca88920
Compare
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) | ||
) |
There was a problem hiding this comment.
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'stemplate_name
andget_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 likeself.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, therequest
can only be accessed in theparent_context
ofget_context_data(parent_context)
- Pros:
- Letting the
Userbar
do the rendering (i.e. viaUserbar(...).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, therequest
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 acceptsrequest
during init rather than inheriting it from the parent context inget_context_data
- Pros:
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.
ca88920
to
489b426
Compare
See also: