Django Testing - check messages for a view that redirects

后端 未结 5 882
我在风中等你
我在风中等你 2021-02-02 10:16

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

5条回答
  •  时光取名叫无心
    2021-02-02 10:34

    I had the same problem when using a 3rd party app.

    If you want to get the messages from a view that returns an HttpResponseRedict (from which you can't access the context) from within another view, you can use get_messages(request)

    from django.contrib.messages import get_messages  
    
    storage = get_messages(request)  
    for message in storage:  
        do_something_with_the_message(message)  
    

    This clears the message storage though, so if you want to access the messages from a template later on, add:

    storage.used = False
    

提交回复
热议问题