在rest1-book-list链接对应的html模板中增加
{% if messages %}
{% for message in messages %}
{{ message }}
{% endfor %}
{% endif %}
可接收到success_message传送的消息。
from django.contrib.messages.views import SuccessMessageMixin
class BookCreateView(SuccessMessageMixin, CreateView):
model = Book
form_class = BookForm
template_name = 'book_form.html'
success_url = reverse_lazy('rest1-book-list')
success_message = '%(title)s was created successfully'
msg=''
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['msg'] = self.msg
return context
reference: https://docs.djangoproject.com/en/2.2/ref/contrib/messages/
网友评论