2014-03-30 1 views
3

플라스크를 배우고 작은 앱을 만들려고합니다. 그래서 처음에 나는 사용하여 CSS 파일없이 시험 : (지연() 발전기의 결과를 얻을 수)플라스크 - 생성기에서 Response (stream_template)를 사용할 때 CSS를 사용하는 방법

return Response(stream_template('login.html', data=delay())) 

그것은 내가 이제이있는 style.css라고하자, 새로운 CSS를 구현하려는 지금 다음 나를 위해 잘 작동을 그리고 정적 폴더에 넣어. HTML 파일에서 내가 가진 :

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

상황 및 대응에 문제가있는 것처럼 코드는 아마 작동하지 않을 것이다, 그러나 정적 수익으로 잘 작동합니다 :

return render_template('login.html') 

내 질문 : 어쨌든 정적 폴더에서 CSS를 사용하여 작동하도록 생성기 (delay() 함수)를 둘 다 가질 수 있습니까? 방금이 문제에 대해 몇 시간을 보냈지 만 아직 답변을 찾을 수 없습니다.

내 stream_template :

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 

감사 에서 인용이 많이

+0

왜 작동하지 않습니까? 그 스타일 태그는 어디에 두 시나요? (템플릿의 상단 또는 하단) –

+0

평소처럼 템플릿의 상단에 넣었습니다. Response를 사용할 때 다음을 얻습니다. RuntimeError : 응용 프로그램 컨텍스트를 푸시하지 않고 URL을 생성하려고 시도했습니다. 응용 프로그램 컨텍스트가 사용 가능할 때 실행되어야합니다. – Kiddo

+0

내가 준 답변을 보면 내 문제가 도움이된다고 생각합니다. –

답변

1

: 귀하의 경우

Note that when you stream data, the request context is already gone the moment the function executes. Flask 0.9 provides you with a helper that can keep the request context around during the execution of the generator: ...

, 당신의 코드는 아마되어야합니다 :

return Response(stream_with_context(stream_template('login.html', data=delay()))) 

또는

return Response(stream_template('login.html', data=stream_with_context(delay()))) 
+0

흥미 롭다. 나는 stream_with_context와 stream_template을 동시에 사용할 수 있다고 생각하지 않았다. 잘 작동하는 cod snippet에 감사드립니다. – Kiddo

관련 문제