본문 바로가기
Django

django) 장고 업로드한 이미지 읽어오기

by 빈스터디 2023. 2. 27.

1. 이미지를 출력하는 페이지의 경로를 지정

 

 

2. views.py를 다음과 같이 입력한다. bid에 맞게 이미지 가져오기

def read(request, bid):
    result = Board.objects.get(id=bid)
    
    context = {
        "result" : result
    }
    return render(request, 'read.html',context)

 

 

3. 이미지 경로인 image.url을 통해서 이미지를 출력한다.

*style은 크기 조정한 것이다.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
{{ result.image.url }} <br>
<img src="{{ result.image.url }}"
     style = "width:150px; height: 200px;"> <br>
</body>
</html>

 

 

4. 다시 한 번 업로드

업로드

 

 

5. 업로드 되었는지 확인하기

 

DB에 boardimage라는 테이블에 업로드 된 이미지가 저장되어 있음

저장되어 있는 모습

 

 

6. http://127.0.0.1:8000/read/1 (= urls.py에서 설정한 대로 서버 / read / board_id)을 입력하면 이미지 출력을 확인할 수 있다.