From ae9bbb40fdee76f7abf5c43da5e631583d58f8b3 Mon Sep 17 00:00:00 2001 From: ibuler Date: Tue, 23 Aug 2016 12:18:19 +0800 Subject: [PATCH] Add flash message template --- apps/templates/_message.html | 7 +++++++ apps/templates/base.html | 1 + apps/users/views.py | 14 ++++++++++++-- 3 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 apps/templates/_message.html diff --git a/apps/templates/_message.html b/apps/templates/_message.html new file mode 100644 index 000000000..8eb061661 --- /dev/null +++ b/apps/templates/_message.html @@ -0,0 +1,7 @@ +{% if messages %} + {% for message in messages %} +
+ {{ message|safe }} +
+ {% endfor %} +{% endif %} diff --git a/apps/templates/base.html b/apps/templates/base.html index 7abe00099..2b07c3227 100644 --- a/apps/templates/base.html +++ b/apps/templates/base.html @@ -20,6 +20,7 @@ {% include '_left_side_bar.html' %}
{% include '_header_bar.html' %} + {% include '_message.html' %} {% block content %}{% endblock %} {% include '_footer.html' %}
diff --git a/apps/users/views.py b/apps/users/views.py index fb7483bdc..9861e533f 100644 --- a/apps/users/views.py +++ b/apps/users/views.py @@ -1,11 +1,14 @@ # ~*~ coding: utf-8 ~*~ -from django.shortcuts import get_object_or_404 +from __future__ import unicode_literals + +from django.shortcuts import get_object_or_404, reverse from django.urls import reverse_lazy from django.db.models import Q from django.views.generic.list import ListView from django.views.generic.edit import CreateView, DeleteView, UpdateView from django.views.generic.detail import DetailView +from django.contrib.messages.views import SuccessMessageMixin from django.conf import settings from .models import User, UserGroup, Role @@ -37,11 +40,12 @@ class UserListView(ListView): return context -class UserAddView(CreateView): +class UserAddView(SuccessMessageMixin, CreateView): model = User form_class = UserAddForm template_name = 'users/user_add.html' success_url = reverse_lazy('users:user-list') + success_message = '添加用户 %s 成功 .' def get_context_data(self, **kwargs): context = super(UserAddView, self).get_context_data(**kwargs) @@ -54,6 +58,12 @@ class UserAddView(CreateView): user.save() return super(UserAddView, self).form_valid(form) + def get_success_message(self, cleaned_data): + return self.success_message % ( + reverse_lazy('users:user-detail', kwargs={'pk': self.object.pk}), + self.object.name, + ) + class UserUpdateView(UpdateView): model = User