简体   繁体   中英

Django message is not working properly using succes message mixin

hi am trying to show message after successful booking but it's not working

this is my views.py

class booking_confirm(CreateView, SuccessMessageMixin, LoginRequiredMixin):
    form_class = booking_form1
    model = Booking
    template_name = "confirm_booking1.html"
    success_message = "booking was created successfully"
    success_url = reverse_lazy("Driver:Driverview")

    def form_valid(self, form, *args, **kwargs):
        booking = get_object_or_404(Loader_post, pk=self.kwargs.get('pk'))
        print(form.cleaned_data)
        bk = form.save(commit=False)
        bk.user = self.request.user
        bk.post = booking
        bk.approve = True
        bk.save()
        return super().form_valid(form)

this is my html code

{% if messages %}
<ul class="messages">
    {% for message in messages %}
        <li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
    {% endfor %}
 </ul>
{% endif %}

views.py

class booking_confirm(CreateView, SuccessMessageMixin, LoginRequiredMixin):
form_class = booking_form1
model = Booking
template_name = "confirm_booking1.html"
messages.info(request,"booking was created successfully")

success_url = reverse_lazy("Driver:Driverview")

html

{% if messages %}
<ul>
{% for message in messages %}
    <li class="{{ message.tags }}">{{ message }}</li>
{% endfor %}
 </ul>
{% endif %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM