0

Been reading and watching quite a bit about integration ReactJS into a Django project. I get the basic concepts of data flowing from Django in the form of JSON via Django REST Framework.

Where tutorials seem to end though is how data flows in the reverse direction and what role, if any do Django forms play? I am thinking input elements are rendered back into JSON and then sent back through Django REST Framework in the form of PUT, PATCH, POST, etc. to update models.

However, when it comes to using user input against Django's built in auth views, I am a bit lost. Does anyone have experience with this that cares to explain?

4
  • Could you please clarify what your project layout is. For example, are you creating a REST API with Django and calling that API from the React project? Commented Feb 2, 2017 at 1:46
  • Yes, that is correct. Using ReactJS for the front-end, so it is my understanding I have to use Django REST Framework to create an API, then have ReactJS take the JSON to render for the front-end. I haven't got that far yet, but it just got me wondering how interaction on the front-end gets passed back into Django. Commented Feb 2, 2017 at 3:13
  • you would make an api call to your Django api via something like fetch. You wouldn't use Django forms at all Commented Feb 2, 2017 at 3:52
  • Your are talking about rendering data from Django into ReactJS, correct? If so, I am talking about how user input from the ReactJS front-end is passed into Django models for updating and adding entries in the database. Commented Feb 2, 2017 at 4:10

1 Answer 1

1

First you have to develop API with Django-Rest-Framework.

Below is the minimal example how to send and receive data in ReactJs

submit data to api:

onSubmit(){
  fetch('https://djangoRest.com/endpoint/', {
  method: 'POST',
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    firstParam: 'yourValue',
    secondParam: 'yourOtherValue',
  })
  })
}

ReactJs fetch data from API

loadData() {    
    fetch('https://djangoRest.com/endpoint/', {
            method: 'GET',
            headers: {
              'Accept': 'application/json',
              'Content-Type': 'application/json',
            }
          })
          .then((response) => response.json())
          .then((responseJson) => {
            if (responseJson.data) {
                this.setState({ Data: responseJson.data });
            }
            if (responseJson.errors) {
              console.log('errors', responseJson.errors)
            }
          })
    }

Ref: https://facebook.github.io/react-native/docs/network.html

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.