From 3368c95ae23b773133c5d7b82505c14e0f4f5d47 Mon Sep 17 00:00:00 2001 From: Irene Sheen Date: Fri, 12 Dec 2025 11:43:41 +0530 Subject: [PATCH] Add messages --- notes/templates/notes/home.html | 1 + notes/templates/notes/messages.html | 7 +++++++ notes/templates/notes/note_form.html | 1 + notes/templates/notes/user_form.html | 3 ++- notes/templates/registration/login.html | 17 ++++++++--------- notes/views.py | 7 +++++++ 6 files changed, 26 insertions(+), 10 deletions(-) create mode 100644 notes/templates/notes/messages.html diff --git a/notes/templates/notes/home.html b/notes/templates/notes/home.html index 235b1dd..f25c20a 100644 --- a/notes/templates/notes/home.html +++ b/notes/templates/notes/home.html @@ -4,6 +4,7 @@ {% block body %}

Dashboard

+ {% include "notes/messages.html" %}

Notes for you

{% if notes %} diff --git a/notes/templates/notes/messages.html b/notes/templates/notes/messages.html new file mode 100644 index 0000000..35500bf --- /dev/null +++ b/notes/templates/notes/messages.html @@ -0,0 +1,7 @@ +{% if messages %} +
+ {% for message in messages %} +
{{ message }} + {% endfor %} +
+{% endif %} diff --git a/notes/templates/notes/note_form.html b/notes/templates/notes/note_form.html index 086c54e..88c9a35 100644 --- a/notes/templates/notes/note_form.html +++ b/notes/templates/notes/note_form.html @@ -4,6 +4,7 @@ {% block body %}

Post a Note

+ {% include "notes/messages.html" %}
{% csrf_token %} {{ form.as_p }} diff --git a/notes/templates/notes/user_form.html b/notes/templates/notes/user_form.html index 5c84dc9..d447a03 100644 --- a/notes/templates/notes/user_form.html +++ b/notes/templates/notes/user_form.html @@ -4,9 +4,10 @@ {% block body %}

Profile

+ {% include "notes/messages.html" %} {% csrf_token %} {{ form.as_p }} - +
{% endblock %} diff --git a/notes/templates/registration/login.html b/notes/templates/registration/login.html index 27c5cc2..ade7428 100644 --- a/notes/templates/registration/login.html +++ b/notes/templates/registration/login.html @@ -6,14 +6,13 @@
- + {% include "notes/messages.html" %} +
+ {% csrf_token %} +

Welcome!

+

Please login to continue

+ {{ form.as_p }} + +
{% endblock %} diff --git a/notes/views.py b/notes/views.py index 3603c55..51983f6 100644 --- a/notes/views.py +++ b/notes/views.py @@ -1,5 +1,6 @@ from datetime import timedelta from typing import Any +from django.contrib import messages from django.contrib.auth.mixins import LoginRequiredMixin from django.urls import reverse_lazy from django.utils import timezone @@ -33,6 +34,7 @@ class PostNoteView(LoginRequiredMixin, CreateView): note.expiry = timezone.now() + timedelta(seconds=note.to_user.expiry_seconds) note.from_user = self.request.user note.save() + messages.success(self.request, "Note has been posted!") return FormMixin.form_valid(self, form) @@ -43,3 +45,8 @@ class ProfileView(LoginRequiredMixin, UpdateView): def get_object(self): return self.request.user + + def form_valid(self, form): + ret = super().form_valid(form) + messages.success(self.request, "Profile has been updated!") + return ret