Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show a successful message with Class Based Views

I, want show a successful message when a row is saved, using Django's messaging framework with Class Based Views, with code shown below, but don't show the message.

Any help would be very much appreciated

#views.py

from django.views.generic import ListView, CreateView, UpdateView, TemplateView
from django.contrib.messages.views import SuccessMessageMixin

class CreateEmployee(SuccessMessageMixin, CreateView):
    model = Employee
    template_name = 'employees/create.html'
    form_class = frmCreate

    def get_success_url(self):
       return reverse('Employees:Create')

    def get_context_data(self, **kwargs):
        contexto = super(CreateEmployee, self).get_context_data(**kwargs)
        contexto['action'] = reverse('Employees:Create')
        return contexto

    success_message = 'Employee successful created'



#template
#create.html

<form action="{{ action }}" method="POST" role="form">
    {% csrf_token %}

    {{ form.as_p }}

    <input type="submit" value="Save" class="btn btn-success">
<form>

{% if messages %}
    <div class="col-lg-3 color03">
        <ul class="messages">
            {% for message in messages %}
                <li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
            {% endfor %}
        </ul>
    </div>
{% endif %}
like image 900
Elio Clímaco Herrera Avatar asked Jul 23 '14 15:07

Elio Clímaco Herrera


People also ask

What is a class based view?

A view is a callable which takes a request and returns a response. This can be more than just a function, and Django provides an example of some classes which can be used as views. These allow you to structure your views and reuse code by harnessing inheritance and mixins.

Are class based views better?

The most significant advantage of the class-based view is inheritance. In the class-based view, you can inherit another class, and it can be modified for the different use cases. It helps you in following the DRY principle. You won't have to write the same code over and over in your boilerplate.


2 Answers

Just use self.request like this:

from django.contrib import messages
messages.add_message(self.request, messages.INFO, 'Hello world.')
like image 61
Alexey Avatar answered Sep 25 '22 20:09

Alexey


#Using django 3.2

#In views.py  

from django.contrib import messages
from .models import CreateEmployer
from django.views.generic import CreateView

class SignUpView(CreateView):
    model = CreateEmployer
    template_name = ‘employee/register_employee.html’
    fields = '__all__'

    # this method will enable your message to display
    # you can also use it to overwrite form data.
    def form_valid(self, form):
        messages.success(self.request, f”Account created successfully”)
        return super().form_valid(form)


#in your urls.py
from .views import SingUpView
from django.urls import path


urlpatterns = [
    path(‘register/‘, SignUpView.as_view(), name=“user-register"),
]


#in your template

 {% if messages %}
   {% for message in messages %}
     <div class="alert alert-{{message.tags}}">
       {{message}}
     </div>
   {% endfor %}
{% endif %}


#I hope this helps anyone facing same problem in the future.
like image 44
Olowu Abayomi Avatar answered Sep 25 '22 20:09

Olowu Abayomi