I have a React frontend which has a state variable that contains an array. I want to pass this state variable onClick to my Django's views.py. What I have so far is something like this:
App.js
const [dataSource, setDataSource] = useState([]); // Where the data is stored in
const requestOptions = {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(dataSource),
};
const handleSubmit = () => {
fetch("http://localhost:8000/insert", requestOptions);
};
<Popconfirm onConfirm={handleSubmit}>
<Button>Submit</Button>
</Popconfirm>
views.py
def insert(request):
if request.method == 'POST':
json_data = request.POST.get()
# I'm not sure what should I be putting as a parameter for .get() method
return render(request, 'frontend/index.html')
urls.py
...
urlpatterns = [
...
path('insert/', views.insert),
...
]
Am I using the right approach? If it's right, what should I be passing as parameters to the get() method?
*P.s. I'm not using DRF for this approach and I'm building the app where Django and React is in one app (not separate) where the frontend is an app of Django.
postmethod suppose to end in/. And check the request.body to receive raw data and then you can use json.loads to convert to dictionary.