Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This works for me (displays all messages):</p> <pre><code>print [m.message for m in list(response.context['messages'])] </code></pre> <p>Also here are a couple of utility methods I have in a test class inherited from Django's TestCase. If you'd prefer to have them as functions, remove the <code>self</code> arguments and replace <code>self.fail()</code>'s with a <code>raise</code>.</p> <pre><code>def assert_message_count(self, response, expect_num): """ Asserts that exactly the given number of messages have been sent. """ actual_num = len(response.context['messages']) if actual_num != expect_num: self.fail('Message count was %d, expected %d' % (actual_num, expect_num)) def assert_message_contains(self, response, text, level=None): """ Asserts that there is exactly one message containing the given text. """ messages = response.context['messages'] matches = [m for m in messages if text in m.message] if len(matches) == 1: msg = matches[0] if level is not None and msg.level != level: self.fail('There was one matching message but with different' 'level: %s != %s' % (msg.level, level)) return elif len(matches) == 0: messages_str = ", ".join('"%s"' % m for m in messages) self.fail('No message contained text "%s", messages were: %s' % (text, messages_str)) else: self.fail('Multiple messages contained text "%s": %s' % (text, ", ".join(('"%s"' % m) for m in matches))) def assert_message_not_contains(self, response, text): """ Assert that no message contains the given text. """ messages = response.context['messages'] matches = [m for m in messages if text in m.message] if len(matches) &gt; 0: self.fail('Message(s) contained text "%s": %s' % (text, ", ".join(('"%s"' % m) for m in matches))) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload