Browse Source

Add flash message template

pull/530/head
ibuler 8 years ago
parent
commit
ae9bbb40fd
  1. 7
      apps/templates/_message.html
  2. 1
      apps/templates/base.html
  3. 14
      apps/users/views.py

7
apps/templates/_message.html

@ -0,0 +1,7 @@
{% if messages %}
{% for message in messages %}
<div class="alert alert-{{ message.tags }}" style="margin: 20px auto 0px">
{{ message|safe }}
</div>
{% endfor %}
{% endif %}

1
apps/templates/base.html

@ -20,6 +20,7 @@
{% include '_left_side_bar.html' %}
<div id="page-wrapper" class="gray-bg">
{% include '_header_bar.html' %}
{% include '_message.html' %}
{% block content %}{% endblock %}
{% include '_footer.html' %}
</div>

14
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 = '添加用户 <a href="%s">%s</a> 成功 .'
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

Loading…
Cancel
Save