I have been writing tests for one of my django applications and have been looking to get around this problem for quite some time now. I have a view that sends messages using django.contrib.messages
for different cases. The view looks something like the following.
from django.contrib import messages
from django.shortcuts import redirect
import custom_messages
def some_view(request):
""" This is a sample view for testing purposes.
"""
some_condition = models.SomeModel.objects.get_or_none(
condition=some_condition)
if some_condition:
messages.success(request, custom_message.SUCCESS)
else:
messages.error(request, custom_message.ERROR)
redirect(some_other_view)
Now, while testing this view client.get
's response does not contain the context
dictionary that contains the messages
as this view uses a redirect. For views that render templates we can get access to the messages list using messages = response.context.get('messages')
. How can we get access messages
for a view that redirects?
redirect(reverse(some_other_view) + '?user_added=true')
– Pacer