私は自分の django アプリケーションの 1 つのテストを書いていて、かなり長い間この問題を回避しようとしていました。django.contrib.messages
さまざまなケースを使用してメッセージを送信するビューがあります。ビューは次のようになります。
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)
現在、このビューの応答をテストしている間、このビューはリダイレクトを使用するため、を含む辞書が含まclient.get
れていません。テンプレートをレンダリングするビューの場合、 を使用してメッセージ リストにアクセスできます。リダイレクトするビューへのアクセスを取得するにはどうすればよいですか?context
messages
messages = response.context.get('messages')
messages