How to send a success message with get_success_url() in Django FormView
Asked Answered
B

4

9

I've got FormView that redirect to a previous page if the form was valid. That's works fine but how can I tell a user that the information has been posted? I want him to see a success message in a modal window after redirect.

I've tried to do it through request.session in get_success_url but it doesn't fit to my goals because a user can submit the form multiple times. So how can I return any message with redirect in get_success_url in FormView?

My FormView

class CatPhotoUploadFormView(FormView):
    template_name = 'blank.html'
    form_class = CatPhotoForm

    def get_success_url(self):
        self.request.session['success_message'] = 'Everything is fine'
        return reverse('cat:detail_cat', args=(self.kwargs['pk'],))

    def form_valid(self, form):
        cat = Cat.objects.filter(id__exact=self.kwargs['pk'])
        for each in form.cleaned_data['attachments']:
            print('****', each, '****', type(each))
            Photo.objects.create(photo_path=each, photo_author=self.request.user, photo_cat = cat[0])
        return super(CatPhotoUploadFormView, self).form_valid(form)
Bunch answered 4/6, 2016 at 17:56 Comment(1)
Have you looked into using Django's messages framework?Pelagian
R
10

Use Django messaging framework for this purpose, change get_success_url with message.

from django.contrib import messages
def get_success_url(self):
    messages.add_message(self.request, messages.INFO, 'form submission success')
    return reverse('cat:detail_cat', args=(self.kwargs['pk'],))

In your template, something like this ( NOTE: remember to pass messages )

{% if messages %}
<ul class="messages">
    {% for message in messages %}
    <li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
    {% endfor %}
</ul>
{% endif %}
Radioactivate answered 5/6, 2016 at 4:39 Comment(2)
Request object is not available in get_success_urlOtolith
Use self.request («Edits must be at least 6 characters»)Edwinedwina
V
6

In fact Django has a ready to use mixin, SuccessMessageMixin, which can be use in class based views to achieve the same purpose.

Like so:

from django.contrib.messages.views import SuccessMessageMixin

class CatPhotoUploadFormView(SuccessMessageMixin, FormView):
    template_name = 'blank.html'
    form_class = CatPhotoForm
    success_message = 'Everything is fine'
    ...

Very clean and straightforward.

Vermicide answered 20/4, 2017 at 10:3 Comment(1)
This is not a correct answer : question is about using messages and using get_success_url() method. success_message looks like ignored in this use case.Edwinedwina
T
2
def get_success_url(self):
    from django.contrib import messages
    messages.success(self.request, 'Yes it works!')
Thornhill answered 10/12, 2019 at 19:14 Comment(1)
The only answer that worked for me. @Renjith Thankachan tip generated an error : int() argument must be a string, a bytes-like object or a number, not 'function' Just added at the end the return reverse (Django 3)Educationist
I
1

This will do the work

def form_valid(self, form):
    print(self.request.POST)
    form.save()
    messages.add_message(
        self.request,
        messages.SUCCESS,
        'User registered sucessfully'
    )
    return super().form_valid(form)
Iberia answered 30/4, 2021 at 3:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.