삽이 부서질 때까지 삽질

flask static file 적용(css, js, image 등) 본문

flask

flask static file 적용(css, js, image 등)

xinfo 2017. 9. 10. 16:00

템플릿 에러를 수정하니 이번엔 css가 적용이 하나도 안되어 있다...


하...

산 넘어 산...


이번에도 에러를 봐보자!!

에러는 콘솔에 친절히 떠있을 것이다.


image, js, css 등 하나도 찾을 수 없다고 나온다...


자 이번에도 doc를 찾아보자

http://flask.pocoo.org/docs/0.10/quickstart/#static-files


위에서 아래와 같은 내용을 볼 수 있을 것이다.


Static Files

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. Just create a folder called staticin your package or next to your module and it will be available at /static on the application.

To generate URLs for static files, use the special 'static' endpoint name:

url_for('static', filename='style.css')

The file has to be stored on the filesystem as static/style.css.


동적인 파일을 적용하기 위해선 static 폴더를 만들고 URL을 수정을 해달라고 한다.



root 폴더에 static 폴더를 만들고, 동적 파일들을 넣어뒀다.




   ---------- 전 ---------- 

    <link rel="stylesheet" href="css/bootstrap.min.css">


   ----------후----------

    <link rel="stylesheet" href="{{ url_for('static', filename='css/bootstrap.min.css') }}">


위와같이 에러 나오는 모든 걸 수정해야한다.

그러면 적용된 멋있는 홈페이지가 되있는 것을 확인할 수 있을 것이다!



추가 내용

{{ ... }} 는 html 에서 변수나 표현식의 결과 값을 의미한다고 한다. html에선 사용하지 않지만, jinja2 에서 템플릿 엔진이 사용할 수 있게 도와준다고 한다.


발췌

http://snacky.tistory.com/6

'flask' 카테고리의 다른 글

jinja2.exceptions.TemplateNotFound 해결법  (0) 2017.09.10
Comments