728x90
728x90

  플라스크 변수 사용  

- 플라스크 서버에서는 html에 변수를 만들어 서버 python에서 값을 넘겨 줄 수 있습니다.

- 변수 사용에는 아래의 코드를 원하는 위치에 추가해 사용합니다.

{{변수명}}

- 변수에 값을 주려면 서버 함수에서 render_template에 변수명에 값을 대입하면 됩니다.

return render_template('hello.html',변수명 = 값)

  예제  

- hello.html 파일을 생성하고, <head>태그 -> <title>태그 안에 변수를 선언합니다.

- <body> 태그 안에 <h1>태그를 추가 하고 그안에 변수를 똑같이 선언해 주겠습니다.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{{title}}</title>
</head>
<body>
    <h1>
        {{title}}
    </h1>
</body>
</html>

- 서버 코드에서 /hello 주소를 라우팅하고 hello() 함수를 선언 return 값으로 render_template('hello.html',title='hello')

를 추가합니다.

 

@app.route("/hello")
def hello():

    return render_template('hello.html',title='hello')

- 이제 /hello로 접속해서 확인해 보겠습니다.

 

- 잘 적용 되었네요!!!!

728x90
728x90
728x90

+ Recent posts