Update your react component code to this, i see you have imported axios but havent't used it to make api call, You should definitely checkout axios official documentation, very useful package
import React, { Component } from "react";
import axios from "axios";
class App extends Component {
constructor() {
this.state({
textAreaValue: "",
});
}
api_call = (data) =>
axios
.post(`http://127.0.0.1:8000/your-api-path`, {
value_to_send: data,
})
.then(function (response) {
console.log(response);
this.setState({textAreaValue:''});
})
.catch(function (error) {
console.log(error);
});
render() {
return (
<div>
<textarea
onChange={(e) => this.setState({textAreaValue: e.target.value})}
value={this.state.textAreaValue}
/>
<button onClick={() => this.api_call(this.state.textAreaValue)}>Click it</button>
</div>
);
}
}
export default App;
Update your viewset to this i.e. use self.request.data received in api request
from django import db
from django.shortcuts import render, HttpResponse
from rest_framework import generics
from .models import Test
from .serializers import TestSerializer
from pymongo import MongoClient
from ast import literal_eval
from rest_framework import viewsets, permissions
# from .models import Test
# from .serializers import TestSerializer
#Test Viewset
class TestViewset(viewsets.ModelViewSet):
request_data = request.data
print(request_data)
client = MongoClient()
db = client.test
# collect = db['state_entry'].find({})
queryset = db['queryTest_test'].find({})
permission_classes = [
permissions.AllowAny
]
serializer_class = TestSerializer
Have a look at django request/response docs here
Checkout this answer here for a similar requirement in backend.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…