You can communicate with your backend using an HTTP request library (e.g axios, fetch, ajax requests, etc)
Communication over HTTP is done through several different verbs (i.e GET, POST, PUT, PATCH, DELETE, etc) I'm agnostic of how your endpoint is handling the request so I'll assume your endpoint expects a POST.
NOTES:
- The example below use axios as HTTP request library for JS
- I'm suggesting a change to your original code to use picts
# urls.py
from django.urls import path
from . import views
url_patterns = [
path('path/to/my_endpoint', views.my_endpoint),
]
# views.py
from django.http import HttpResponse
def my_endpoint(request):
pict = request.POST.getlist('pict[]')
process_pict(pict)
return HttpResponse('Success')
In this case you can send the request to your server as follows:
// api.js
import axios from 'axios'
export function sendPict(pict) {
const payload = {'pict[]': pict}
return axios.post('path/to/my_endpoint', payload)
}
Then you can send your pict anywhere from your app:
import sendPict from './api'
const pict = [];
pictureEl.addEventListener(`click`, function () {
console.log(`Take Pic`);
pict.push(webcam.snap());
console.log(pict);
console.log('sending pict to server')
sendPict(pict)
.then(response => console.log(`The success server response is: ${response}`))
.catch(error => alert(`Oh noes: ${error}`))
});