4

Is there any way I can use Django forms inside a ReactJS script, like include {{ form }} in the JSX file?

I have a view which displays a from and it is rendered using React. When I load this page from one page the data in these fields should be empty, but when I hit this view from another page I want date to be prefilled in this form. I know how to do this using Django forms and form views, but I am clueless where to bring in React.

1 Answer 1

2

The {{ form }} statement is relative to Django template. Django templates responsible for rendering HTML and so do React, so you don't have to mix the two together.

What you probably want to do is to use the Django form validation mechanism server side, let React render the form client-side. In your Django view, simply return a JSON object that you can use in your React code to initialize your form component.

Sign up to request clarification or add additional context in comments.

6 Comments

This needs a ton of boilerplate. I've had this idea in the back of my head to dynamically generate React forms from Django forms.
In fact, there no need to so much boilerplate, the are just couple of things to do : make sure to return a json-like object from the Django and printing it inside a <script/> tag that react will use. The form just need to be sent through a post request with the csrfmiddlewaretoken and that's it !
I am kind of new to the web development, can u please elaborate or point in the to some reading material that is useful
Sure, can you add in your question the code of your django form and the django view that handle this form ?
Thanx for your help, but I am kind of out of time so I resorted to simple java script over React
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.