

from django.core.urlresolvers import reverse
from django.views.generic import CreateView
from django.utils.translation import ugettext_lazy as _
from django.contrib import messages

from .forms import ContactForm


class ContactView(CreateView):
    template_name = 'contacts/contact.html'
    form_class = ContactForm

    def get_success_url(self):
        return reverse("qs_contacts:contact")

    def form_valid(self, form):
        messages.success(self.request, _("Your message was sent successfully!"))
        return super(ContactView, self).form_valid(form)



from django.core.urlresolvers import reverse
from django.views.generic import CreateView
from django.utils.translation import ugettext_lazy as _
from django.contrib import messages

from .forms import ContactForm


class ContactView(CreateView):
    template_name = 'contacts/contact.html'
    form_class = ContactForm

    def get_success_url(self):
        return reverse("qs_contacts:contact")

    def form_valid(self, form):
        valid = super(ContactView, self).form_valid(form)
        messages.success(self.request, _("Your message was sent successfully!"))
        return valid

Set flash message *after* message sending
Set flash message *after* message sending

  
  
  from django.core.urlresolvers import reverse
  from django.views.generic import CreateView
  from django.utils.translation import ugettext_lazy as _
  from django.contrib import messages
  
  from .forms import ContactForm
  
  
  class ContactView(CreateView):
      template_name = 'contacts/contact.html'
      form_class = ContactForm
  
      def get_success_url(self):
          return reverse("qs_contacts:contact")
  
      def form_valid(self, form):
+         valid = super(ContactView, self).form_valid(form)
          messages.success(self.request, _("Your message was sent successfully!"))
-         return super(ContactView, self).form_valid(form)
+         return valid
  
Set flash message *after* message sending
## Code Before:


from django.core.urlresolvers import reverse
from django.views.generic import CreateView
from django.utils.translation import ugettext_lazy as _
from django.contrib import messages

from .forms import ContactForm


class ContactView(CreateView):
    template_name = 'contacts/contact.html'
    form_class = ContactForm

    def get_success_url(self):
        return reverse("qs_contacts:contact")

    def form_valid(self, form):
        messages.success(self.request, _("Your message was sent successfully!"))
        return super(ContactView, self).form_valid(form)

## Instruction:
Set flash message *after* message sending
## Code After:


from django.core.urlresolvers import reverse
from django.views.generic import CreateView
from django.utils.translation import ugettext_lazy as _
from django.contrib import messages

from .forms import ContactForm


class ContactView(CreateView):
    template_name = 'contacts/contact.html'
    form_class = ContactForm

    def get_success_url(self):
        return reverse("qs_contacts:contact")

    def form_valid(self, form):
        valid = super(ContactView, self).form_valid(form)
        messages.success(self.request, _("Your message was sent successfully!"))
        return valid
