React — Django how to add avatar?


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
15 views
Good evening, need to do add the avatar on the front has a input c type file and a submit button to the server.
On the server read the parameter and add the avatar field in the user model. Get the error the name is not serialized - nature_gora.jpg> is not JSON serializable
fileChangedHandler(e) { this.setState({selectedFile: e.target.files[0]}) } uploadHandler() { console.log(this.state.selectedFile); axios.post("/api/add_user_avatar/", { avatar: this.state.selectedFile.name }) .then((res) => { this.setState({showUploadInput: false}) }) .catch(error => { console.log(error); }); } @csrf_exempt def add_user_avatar(request): if request.method == "POST": user = UserAccount.objects.get(id=request.user.id) data_unicode = request.body.decode("utf-8") data = json.loads(data_unicode) user.avatar = "nature_gora.jpg" user.save() return JsonResponse({ avatar: user.avatar }, safe=False)

What is missing in the backend?
by | 15 views

1 Answer

0 like 0 dislike
you js need to create a FormData object, push the file into it, and the backend is already working with form
\rWhat a beautiful file uploader for vue?
by

Related questions

0 like 0 dislike
1 answer
0 like 0 dislike
2 answers
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
110,608 questions
257,186 answers
0 comments
35,389 users