Django Rest Framework using Viewsets with different renderers

246 Views Asked by At

does it make sense to use Django Rest Framework also to render my HTML Code with Bootstrap and so on...? If no, what is the best solution to split the API endpoints and HTML view? I exposed an API with DRF Viewsets and JSON serializers. It's working pretty well. Additionally I would like to add a HTML rendered version of this API and design a HTML form for it. Both API and HTML rendered version are supposed to run on the same machine.

Thanks in advance

1

There are 1 best solutions below

3
Tom Carrick On

You can. Nothing's stopping you, and this is how DRF's own browsable renderer works. Whether it makes sense depends on your use case. If your API is the primary thing your app produces, and the HTML is just there to support it, give usage examples, etc. then maybe this is useful and makes sense, as it will always be up to date with your API.

However if the purpose of your API is to provide data (and possibly ways to mutate that data) for your website, the more typical thing to do would be to render your API only in JSON and then have your app consume it, either by Django rendered templates with javascript that fetches what it needs, or by using a frontend framework such as Vue or React.