question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Users should be able to configure API renderer_classes

See original GitHub issue

Issue Summary

Users should be able to configure API renderer_classes, currently these are hardcoded to [JSONRenderer, BrowsableAPIRenderer]: https://github.com/wagtail/wagtail/blob/1958bba2476c32069acafd6837514749a4d3ceea/wagtail/api/v2/views.py#L26

Steps to Reproduce

Configure pages endpoint as usual, but do not include rest_framework in the INSTALLED_APPS. Try to open configured endpoint in the browser, the app will throw an exception:

Internal Server Error: /rest/p/
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/usr/local/lib/python3.8/site-packages/django/core/handlers/base.py", line 145, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/usr/local/lib/python3.8/site-packages/django/core/handlers/base.py", line 143, in _get_response
    response = response.render()
  File "/usr/local/lib/python3.8/site-packages/django/template/response.py", line 105, in render
    self.content = self.rendered_content
  File "/usr/local/lib/python3.8/site-packages/rest_framework/response.py", line 70, in rendered_content
    ret = renderer.render(self.data, accepted_media_type, context)
  File "/usr/local/lib/python3.8/site-packages/rest_framework/renderers.py", line 723, in render
    template = loader.get_template(self.template)
  File "/usr/local/lib/python3.8/site-packages/django/template/loader.py", line 19, in get_template
    raise TemplateDoesNotExist(template_name, chain=chain)
django.template.exceptions.TemplateDoesNotExist: rest_framework/api.html

Technical details

  • Python Version: 3.8.2
  • Django Version: 3.0
  • Wagtail Version: 2.9

Currently I’m using this nasty trick:

from django.conf import settings
from wagtail.api.v2.views import PagesAPIViewSet
from wagtail.api.v2.router import WagtailAPIRouter

if not settings.DEBUG:
    from rest_framework.renderers import JSONRenderer

    class ProdPagesAPIViewSet(PagesAPIViewSet):
        renderer_classes = [JSONRenderer]

    PagesAPIViewSet = ProdPagesAPIViewSet

# Create the router. "wagtailapi" is the URL namespace
api_router = WagtailAPIRouter("wagtailapi")

api_router.register_endpoint("p", PagesAPIViewSet)

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:3
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
m-aciekcommented, Apr 28, 2022

Hi, any updates on that?

It forces us to enable DRF browsable API, otherwise there’s an error raised, see https://stackoverflow.com/questions/61961099/wagtail-api-v2-django-template-exceptions-templatedoesnotexist.

It’s somewhat contrary to documentation which states

Optionally, you may also want to add rest_framework to INSTALLED_APPS. This would make the API browsable when viewed from a web browser but is not required for basic JSON-formatted output.

https://docs.wagtail.org/en/stable/advanced_topics/api/v2/configuration.html#enable-the-app

1reaction
marteinncommented, May 25, 2020

Many thanks @narun4sk and @phptek. This is a design decision so I will raise the issue with the core team, will report back here when we have discussed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Renderers - Django REST framework
The default set of renderers may be set globally, using the DEFAULT_RENDERER_CLASSES setting. For example, the following settings would use JSON as the...
Read more >
Renderers - Django REST framework - Tom Christie
The default set of renderers may be set globally, using the DEFAULT_RENDERER_CLASSES setting. For example, the following settings would use YAML as the...
Read more >
django - How to override the renderer_classes to send extra ...
So how we can achieve that. ... I hope following code will solve your problem. ... user = serializer.validated_data['user'] token, ...
Read more >
Schemas - Django REST framework中文站点
renderer_classes : May be used to pass the set of renderer classes that can be used to render the API root endpoint. Customizing...
Read more >
The form rendering API - Django documentation
Forms and widgets can specify custom renderer classes. ... By specifying a custom form renderer and overriding form_template_name you can adjust the default ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found