간단한 Flask 앱을 쓰고 있는데 첫 번째 페이지는 사용자 이름/비밀번호를 확인하고 유효성을 확인한 다음 session.logged_in을 true로 업데이트합니다. 확인을 위해 출력물을 사용하고 session.logged_in은 실제로 "True"로 설정됩니다. 로그인 한 후에는 다음과 같은 다른 링크를 탐색하는 html 페이지가 있습니다 : 테이블 목록 (html 파일에는 {{url_for ('table_list')}} 링크가 있습니다.) 그러나 foremanFlask - session.logged_in 업데이트되지 않았습니다.
from flask import Flask, stream_with_context, request, Response, url_for, render_template, flash, session
...
app = Flask(__name__)
app.debug = True # Enable debug-mode
app.secret_key = 'nosecret'
def stream_template(template_name, **context):
app.update_template_context(context)
t = app.jinja_env.get_template(template_name)
rv = t.stream(context)
# uncomment if you don't need immediate reaction
##rv.enable_buffering(5)
return rv
@app.route('/')
def index():
return render_template('signin.html')
@app.route('/', methods=['POST'])
def login():
# Read from the from input
usrn = request.form['username']
pswd = request.form['password']
if validate_login(usrn, pswd):
session['logged_in'] = True
print 'login1:', session.logged_in
sys.stdout.flush()
return Response(stream_with_context(stream_template('index.html', data=genData())))
else:
return Response(stream_template('fail.html', code=pswd, username=usrn))
.....
@app.route('/listtable')
def list_table():
print 'login2:', session.logged_in
sys.stdout.flush()
return Response(stream_with_context(stream_template('listtables.html', loginsession=session.logged_in)))
콘솔의 결과는 다음과 같습니다 :
login1: True
login2: none
나는 그 링크를 클릭 session.logged_in는 없음
내 파이썬 코드가 (중 참 또는 거짓되지 않음)입니다 내가 현재 가지고있는 세션을 어떻게 든 부러 뜨리는 컨텍스트로 스트리밍하기 때문에 문제가 있다고 생각합니다. 이자형. 누구나이 문제를 해결하는 방법에 대한 아이디어가 있습니까? 정말 고맙습니다.
Work-Around : 내 문제는 무엇인지 모르지만 새로운 클래스 MySession을 만들고 내 세션에서 logged_in 상태를 유지하는 부울을 가지고 있습니다. MySession 클래스를 사용하여 현재 세션과 정보를 업데이트 할 수 있습니다. 왜 Flask 세션이 나를 위해 작동하지 않는지 확실하지 않습니다.
해답을 주셔서 감사하지만 내 목표는 플라스크를 배우는 것이므로 다른 패키지를 사용하고 싶지 않습니다. – Kiddo
속성 대신 dict 항목으로 액세스하려고하면 어떻게됩니까? – adarsh
세션 [ 'logged_in']을 인쇄하려고했으나 두 번째 인쇄의 경우 여전히 "없음"입니다. – Kiddo