django react upload image

ash3767·2020년 10월 10일
0

Django

목록 보기
30/35

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개의 댓글