-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
TemplateHTMLRenderer - TypeError - context must be a dict rather than ReturnList. #5236
Comments
Thanks for raising this. |
Example stacktrace:
I think it may only be the case if |
I just ran into this. Is there a workaround? or a local fix I can apply for development? |
@kezabelle @tomchristie As far as I understand this issue, irrespective of the change in Django 1.10+ mentioned originally by @Eskimon, the @kezabelle You are right in saying that simply ensuring (somehow) that dicts are returned will make the renderer work. As far as the Django 1.10+ issue is concerned, the change suggests using native dicts instead of TL;DR: Does not look like a bug to me. |
FWIW I expected a TemplateHTMLRenderer to behave like the API UI.
Apparently, this is not the expected behavior. It's also non-obvious why (or even how) paging magically changes an unacceptable list into a shorter but acceptable "list". |
I stand by my "intuitive" argument, but dug through the code to figure out why paging "fixes" the issue. Obviously, I'm with the OP that this seems like a valid use case (albeit not a new issue). While the pagination class seems like the right place to handle this, I don't think it's practical:
This militates for a fix that is limited to the TemplateHTMLRenderer. The two options I see are:
Note that the documentation for TemplateHTMLRenderer states that that:
I imagine there are many unserialized objects that are not |
... and for anyone who needs a workaround, add a line to your view like:
or, mimicking my recommended fix (at
|
I seem to have come across this issue as well. From my point of view it's a bit baffling - with a Thank you to @ambsw-technology for the workarounds. |
PR to improve the documentation is welcomed :) |
This partially addresses encode#5236 to clarify that the response from a view may need to be modified to provide TemplateHTMLRenderer with a dict for it to use. I do not completely understand the issue in encode#5236 so the wording could possibly be improved.
Okay. Would be useful to have someone who understands this issue better give some input on #6095. |
I have been bitten by this this week. And agree with the above - it doesn't feel like expected behaviour - nor is it clear what is happening immediately. I'm quite happy to submit a PR which would wrap the list in a dict with a key of But I do want to know if this is even desirable for the maintainers - given this issue has been left open for 2 years. |
FYI I've worked around this for now like so: from rest_framework.renderers import TemplateHTMLRenderer
class MyHTMLRenderer(TemplateHTMLRenderer):
def get_template_context(self, *args, **kwargs):
context = super().get_template_context(*args, **kwargs)
if isinstance(context, list):
context = {"items": context}
return context Which gives me the list of objects as an "items" object in my context. |
@xordoquy, so I opened a PR two years ago to improve the documentation as you suggested. A review and merge would be welcomed. 😜 |
Hello there, just jumped into this problem and found an unanswered question about it in stackoverflow: https://stackoverflow.com/q/60816455/5750078 I did not find an answer so after some debugging the DRF found out a workaround similar to those proposed here. Also wrote it as my own answer in that SO page. This is still opened right? wouldn't a slight change in TemplateHTMLRenderer's get_template_context method suffice to fix it? |
Just got bitten by this. Interesting that the DRF docs which are usually very comprehensive do not cover this at all. If TemplateHTMLRenderer is not meant to work in certain scenarios by design, (at least not without tweaking) would be good to have some docs at a minimum around this. |
From my limited understanding, I did try to clarify the docs myself: #6095 |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
People were still complaining about this last year. It would be good to have input from the maintainers. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
There's a PR that seems to address this that's open. |
Okay, so #8569 changes the behaviour more than I'd like.
|
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Checklist
master
branch of Django REST framework.Steps to reproduce
In Django 1.8+, the template's render method takes a dictionary for the context parameter. Support for passing a Context instance is deprecated, and gives an error in Django 1.10+ (source1 / source2).
When adding a renderer to a
generics.ListCreateAPIView
, aTypeError: context must be a dict rather than ReturnList.
pop.To reproduce, create a simple Model+Serializer to fill data in a
generics.ListCreateAPIView
. Add arenderer_classes = [TemplateHTMLRenderer]
to the ListView and try to access the page from a browser to render the template. Should crash (error 500 as an Exception is raised).Expected behavior
The template should be rendered.
Actual behavior
A
TypeError
is thrown becausecontext must be a dict rather than ReturnList
The text was updated successfully, but these errors were encountered: