django react upload image

BackEnd_Ash.log·2020년 10월 10일
1

models


class Book(models.Model):
    title = models.CharField(max_length=50 , null=True)
    cover = models.ImageField(blank=True , null=True , upload_to ="post")
     
     class Meta:
     	db_table="books"

views


class BookView(View):
    def get(self, request):
       data = Book.objects.values()
       return JsonResPonse({"data":data},status=200)
       
    def post(self, request):
    	data = json.loads(request.body)
        
        title = data['title']
        cover = data['cover']
        
        Book(
        title = title,
        cover = cover
        ).save()
        
        return HttpResponse(status=200)

react

import React , {useState} from "react";
import axiso from "axios";

const [title , setTitle] = useState("");
const [cover , setCover] = useState();

const newBook =() =>{
 let data {
   title : title ,
   cover : cover
 }
 axios.
 post("http://localhost:8000/books" , data)
 .then(res => {
   console.log(res);
   
 })
 .catch(err=> console.log(err));
}

<label>
    Title
    <input type="text" value={title} onChange={(e)=>setTitle(e.target.value)}/>
</label>
<br/>
   <label>
	 Cover
	<input type="file" onChange={(e)=>setCover(e.target.files[0])}/>
   </label>
<br/>
<button onClick={newBook}>button</button>
profile
꾸준함이란 ... ?

0개의 댓글