Connecting REST APIs with frontend components is often a time-consuming task. Developers typically grapple with crafting HTML forms, implementing val``idation (using libraries like Yup, Zod, or Joi), and integrating them with the API. This process can be significantly streamlined by leveraging the power of reusable components and Django DRF's exceptional capabilities.
In this article, we'll explore how to expedite this process and enhance development efficiency. By combining well-structured reusable components with Django DRF's robust features, you can dramatically reduce development time and create a more maintainable codebase.
Discover how to supercharge your frontend development with our expert Django development services.
We first create a django Model that will be used to store the data. lets use a Books API model.
Language: python
Path: books/models.py
We need a serializer to convert the model to JSON.
Language: python
Path: books/serializers.py
The view for this file is located in books/views.py
Language: python
Path: books/views.py
the urls for the API are created in the same file.
Language: python
Path: books/urls.py
This is the basic setup on the django backend. this should give us a usable API endoint /api/books/
. (based on how we setup the project)
We are now ready to start building our frontend. We will use React and tailwindCSS build our frontend.
reference: https://tailwindcss.com/docs/installation
For forms we will be using the react-hook-form
library.
Lets start by creating a new form component.
Language: javascript
Path: components/books/forms.js
Lets try and fix these problems.
Lets start by creating input components which play nice with react hook forms and are reusable.
Language: javascript
Path: components/forms/inputs.js
lets build a form component that will be used to create new dyanamic forms composed of the above components.
Language: javascript
Path: components/forms/form.js
Lets build a dynamic form that will be used to create new dyanamic forms composed of the above components.
Language: javascript
Path: components/forms/dynamic-form.js
This component will be used to create dynamic forms. when we pass in a object array of fields, it will create a form with the fields. fields array will typically look like this
If we send a request using OPTIONS
method to the api, django will return the expected shape of data it will accept. if we send this to our books api, it will return the following
Language: javascript
Path: components/forms/form-data.js
We can use this JSON to create a dynamic form. But first we need transform the JSON to a form array. that we are expecting to see in the form.
Here we will do some transformation on the JSON to create a form array.
Language: javascript
Path: pages/sample.js
lets take a look at buildForm
function. which does all the heavy lifting.
this function takes in a JSON schema from the django DRF API
and returns a form array and a schema.
Language: javascript
Path: components/forms/build-form.js
we are using generateYupSchema
to generate a yup schema from the JSON schema.
Language: javascript
Path: components/forms/yupSchemaGenerator.js
On the sample page, we have demonstrated how to use the AutoForm
component.
We have also defined a url
variable. by changing this url
we can get form from another API like /api/authors/
, /api/users/
, etc. without having to write separate forms for them.
simply change the url
and we are on our way to create Dynamic forms driven by our API.