1

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

Janusz Piwek
  • 69
  • 1
  • 4
  • Keen to get an answer to this - I have tried the same thing myself and run into strange errors when trying to get DRF to deliver data alternately to JSON output, and (where appropriate) HTML templates. – Hayden Eastwood May 03 '20 at 09:16

1 Answers1

0

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.

Tom Carrick
  • 6,349
  • 13
  • 54
  • 78
  • My use-case is the following: -lightweight Embedded Linux system (500MHz 128MB RAM) with peripherals (peripherals: relays, sensors). Sensor data is gathered continously and stored in sqlite3 -I want to have a basic local website with --login/logout, User roles with different priviliges (at least 2), create/delete new users just for Admin, Staff user --IP address, DHCP configuartion (ethernet, Wifi), --sensor data charts with e.g. chartJS, (daily, weekly) --CRON setup to add schedules when relays are triggered -expose REST API with similar features for mobile apps, desktop apps, – Janusz Piwek May 03 '20 at 13:26
  • For that I would personally go with one of the latter two options. Using a frontend framework might make sense on a low-powered device like that as the browser will end up doing a lot of the effort, rather than the system it's hosted on, and most computers/phones are going to be more powerful than that. – Tom Carrick May 03 '20 at 14:26
  • I found a nice blog about DRF + React. – Janusz Piwek May 05 '20 at 12:24