Add success message on object creation with link to object

This commit is contained in:
Edgar P. Burkhart 2023-11-25 11:28:20 +01:00
parent 76ac6bc7fb
commit 8dd29be8bf
Signed by: edpibu
GPG key ID: 9833D3C5A25BD227
2 changed files with 19 additions and 1 deletions

View file

@ -88,6 +88,11 @@
{% endspaceless %} {% endspaceless %}
{% endblock %} {% endblock %}
<main id="main"> <main id="main">
{% if messages %}
<ul>
{% for message in messages %}<li>{{ message }}</li>{% endfor %}
</ul>
{% endif %}
{% block body %}{% endblock %} {% block body %}{% endblock %}
</main> </main>
</body> </body>

View file

@ -1,8 +1,10 @@
from account.models import Account from account.models import Account
from category.models import Category from category.models import Category
from django.contrib import messages
from django.contrib.auth import views as auth_views from django.contrib.auth import views as auth_views
from django.contrib.auth.mixins import LoginRequiredMixin from django.contrib.auth.mixins import LoginRequiredMixin
from django.urls import reverse_lazy from django.urls import reverse_lazy
from django.utils.html import format_html
from django.views.generic import ( from django.views.generic import (
CreateView, CreateView,
DeleteView, DeleteView,
@ -55,7 +57,18 @@ class NummiCreateView(UserMixin, CreateView):
return super().form_valid(form) return super().form_valid(form)
def get_success_url(self): def get_success_url(self):
return self.next or super().get_success_url() surl = super().get_success_url()
messages.success(
self.request,
format_html(
"<a href='{surl}'>{name}</a> {msg}",
surl=surl,
name=self.object,
msg="was created successfully",
),
)
return self.next or surl
class NummiUpdateView(UserMixin, UpdateView): class NummiUpdateView(UserMixin, UpdateView):