[Flask] 프로젝트 html 이미지 소스 경로 설정

happypath·2021년 11월 17일
0

Flask

목록 보기
3/10

jinja 템플릿을 이용해서, local에 저장된 이미지를 상대경로로 불러오도록 img src에 지정을 해 줬는데 페이지에서 이미지를 불러오지 못하는 에러.


1. img는 static폴더 안에!

처음에 static 밖에 src폴더를 위치해 놨었는데, 전부 static 안에 있어야 한다는 스택오버플로우 답변을 보았다.
[스택오버플로우 해당 답변]

Dynamic web applications also need static files. That’s usually where the CSS and JavaScript files are coming from. Ideally your web server is configured to serve them for you, but during development Flask can do that as well (https://stackoverflow.com/questions/28207761/where-does-flask-look-for-image-files)

2. 상대 경로 정확하게 체크

저장된 이미지 경로를 정확하게 넣었는지 다시 한 번 체크

> Bonus

상대경로로 하는 방법 이외에, url_for을 사용하여 폴더 지정을 해 주는 방식도 있었다.

<img src="{{url_for('static', filename='1.jpg')}}" />

나는 진자템플릿 변수를 써야했으므로... 포맷팅..!
진자 안에 진자 안에 진자 안에....ㅋㅋㅋㅋㅋㅋ

{% for book in book_list%}
<img src="{{ url_for('static', filename='src/img/{}.png'.format(book.id)) }}" width="100%" height="225" alt="book_image">
{% endfor %}

스택오버플로우 참고

0개의 댓글