美文网首页
CBV成功消息传值

CBV成功消息传值

作者: soyonhyde宝雯 | 来源:发表于2020-03-18 01:09 被阅读0次

    在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/

    相关文章

      网友评论

          本文标题:CBV成功消息传值

          本文链接:https://www.haomeiwen.com/subject/znufyhtx.html