重定向后,Flask闪存中断

时间:2017-09-30 09:11:34

标签: python flask

在我的测试用例中,

with self.client:
  r = self.client.get('/admin/')
  print get_flashed_messages()

我看到以下输出:

[u"You must be signed in to access 'http://Testing/admin/'."]

但是,当我设置follow_redirects=True时,

with self.client:
  r = self.client.get('/admin/', follow_redirects=True)
  print r.data

使用此模板:

  <div class='container'>
    <div id="main-div" class="with-margins">
      FLASH MESSAGE HERE: {{ get_flashed_messages() }}

我看到以下输出:

 <div class='container'>
    <div id="main-div" class="with-margins">

      FLASH MESSAGE HERE: []

有关为何发生这种情况的任何想法?

CODE:

class AuthTestConfig(TestingConfig):
  TESTING = False
  LOGIN_DISABLED = False

class AccessControlTest(unittest.TestCase):

  def setUp(self):
    self.app = create_app(AuthTestConfig)
    self.client = self.app.test_client()

  def tearDown(self):
    pass

  def test_anon_visits_member_redirects_to_login(self):
    from flask import get_flashed_messages

    with self.client:
      r = self.client.get('/admin/', follow_redirects=True)
      #print get_flashed_messages()
      print r.data

1 个答案:

答案 0 :(得分:0)

来自doc的摘录:

  

闪烁系统基本上可以录制消息   请求结束并访问下一个请求并仅访问下一个请求

因此,当您渲染模板时,get_flashed_messages会从会话中删除闪烁的消息,因此您无法在测试用例中访问它。所以你应该关闭渲染(在TestCase中render_templates = False)