原文地址:https://mypython.me
用户管理功能,包含用户添加、列表展示、编辑、删除四大功能。下面我们一一揭晓。
用户添加
我们先实现用户添加功能,我们现在urls.py下添加相关的路由
代码语言:txt
复制
path('user_add/', views.UserAddView.as_view(), name='user_add'),
path('user_list/', views.UserListView.as_view(), name='user_list'),
path('user_edit/<int:pk>',views.UserEditView.as_view(), name='user_edit'),
path('user_delete/', views.user_delete, name='user_delete'),
用户添加的视图类是UserAddView
代码语言:txt
复制
class UserAddView(SuperUserRequiredMixin, generic.View): def get(self, request): form = UserAddForm() return render(self.request, 'myadmin/user_add.html', {'form': form})
def post(self, request): form = UserAddForm(data=request.POST) if form.is_valid(): user = form.save(commit=False) password = form.cleaned_data.get('password') user.set_password(password) user.save() return render(self.request, 'myadmin/user_add_success.html') return render(self.request, 'myadmin/user_add.html', {'form': form})</code></pre></div></div><p>这是一个普通的视图类,通过get()和post()来完成用户添加的功能,get里面负责页面的展示,post里面负责逻辑处理。在get中,初始化form为UserAddForm,因为添加的用户是有类别的,所以我们在UserAddForm中应用了is_staff字段来表示管理员。在post中,我们通过user.set_password(password)来设置新密码。user.save()来保存记录到数据库。保存成功后会跳转到myadmin/user_add_success.html页面。</p><figure class=""><div class="rno-markdown-img-url" style="text-align:center"><div class="rno-markdown-img-url-inner" style="width:100%"><div style="width:100%"><img src="https://cdn.static.attains.cn/app/developer-bbs/upload/1722995623878157944.png" /></div></div></div></figure><h4 id="c87ku" name="%E7%94%A8%E6%88%B7%E5%88%97%E8%A1%A8">用户列表</h4><p>用户添加成功后,当你点击用户列表,即可看到用户列表数据,用户列表我们配置的路由是</p><div class="rno-markdown-code"><div class="rno-markdown-code-toolbar"><div class="rno-markdown-code-toolbar-info"><div class="rno-markdown-code-toolbar-item is-type"><span class="is-m-hidden">代码语言:</span>txt</div></div><div class="rno-markdown-code-toolbar-opt"><div class="rno-markdown-code-toolbar-copy"><i class="icon-copy"></i><span class="is-m-hidden">复制</span></div></div></div><div class="developer-code-block"><pre class="prism-token token line-numbers language-txt"><code class="language-txt" style="margin-left:0">path('user_list/', views.UserListView.as_view(), name='user_list'),</code></pre></div></div><p>使用的是UserListView视图类,该类是继承自ListView通用视图类的。因此 只需要我们简单的配置几下,就能实现列表功能。UserListView代码如下</p><div class="rno-markdown-code"><div class="rno-markdown-code-toolbar"><div class="rno-markdown-code-toolbar-info"><div class="rno-markdown-code-toolbar-item is-type"><span class="is-m-hidden">代码语言:</span>txt</div></div><div class="rno-markdown-code-toolbar-opt"><div class="rno-markdown-code-toolbar-copy"><i class="icon-copy"></i><span class="is-m-hidden">复制</span></div></div></div><div class="developer-code-block"><pre class="prism-token token line-numbers language-txt"><code class="language-txt" style="margin-left:0">class UserListView(AdminUserRequiredMixin, generic.ListView): model = User template_name = 'myadmin/user_list.html' context_object_name = 'user_list' paginate_by = 10 q = '' def get_context_data(self, *, object_list=None, **kwargs): context = super(UserListView, self).get_context_data(**kwargs) paginator = context.get('paginator') page = context.get('page_obj') page_list = get_page_list(paginator, page) context['page_list'] = page_list context['q'] = self.q return context def get_queryset(self): self.q = self.request.GET.get("q", "") return User.objects.filter(username__contains=self.q).order_by('-date_joined')</code></pre></div></div><p>我们知道ListView是有多个回调函数的,这里就是通过get_context_data()和get_queryset()回调函数来实现列表中的功能的。在get_context_data()中实现了列表分页功能,在get_queryset()中实现了搜索功能。</p><p>配置的模板是myadmin/user_list.html,显示效果如下</p><figure class=""><div class="rno-markdown-img-url" style="text-align:center"><div class="rno-markdown-img-url-inner" style="width:100%"><div style="width:100%"><img src="https://cdn.static.attains.cn/app/developer-bbs/upload/1722995624036127099.png" /></div></div></div></figure><h4 id="67ru9" name="%E7%94%A8%E6%88%B7%E7%BC%96%E8%BE%91">用户编辑</h4><p>当你点击编辑按钮的时候,即可进入编辑页面。对应的路由是</p><div class="rno-markdown-code"><div class="rno-markdown-code-toolbar"><div class="rno-markdown-code-toolbar-info"><div class="rno-markdown-code-toolbar-item is-type"><span class="is-m-hidden">代码语言:</span>txt</div></div><div class="rno-markdown-code-toolbar-opt"><div class="rno-markdown-code-toolbar-copy"><i class="icon-copy"></i><span class="is-m-hidden">复制</span></div></div></div><div class="developer-code-block"><pre class="prism-token token line-numbers language-txt"><code class="language-txt" style="margin-left:0">path('user_edit/<int:pk>',views.UserEditView.as_view(), name='user_edit'),</code></pre></div></div><p>即UserEditView</p><div class="rno-markdown-code"><div class="rno-markdown-code-toolbar"><div class="rno-markdown-code-toolbar-info"><div class="rno-markdown-code-toolbar-item is-type"><span class="is-m-hidden">代码语言:</span>txt</div></div><div class="rno-markdown-code-toolbar-opt"><div class="rno-markdown-code-toolbar-copy"><i class="icon-copy"></i><span class="is-m-hidden">复制</span></div></div></div><div class="developer-code-block"><pre class="prism-token token line-numbers language-txt"><code class="language-txt" style="margin-left:0">class UserEditView(SuperUserRequiredMixin, generic.UpdateView): model = User form_class = UserEditForm template_name = 'myadmin/user_edit.html' def get_success_url(self): messages.success(self.request, "保存成功") return reverse('myadmin:user_edit', kwargs={'pk': self.kwargs['pk']})</code></pre></div></div><p>同样是继承自UpdateView,仅需要配置好model、form_class、template_name即可,简单至极。</p><p>效果如下</p><figure class=""><div class="rno-markdown-img-url" style="text-align:center"><div class="rno-markdown-img-url-inner" style="width:100%"><div style="width:100%"><img src="https://cdn.static.attains.cn/app/developer-bbs/upload/1722995624459850223.png" /></div></div></div></figure><h4 id="eg6sb" name="%E7%94%A8%E6%88%B7%E5%88%A0%E9%99%A4">用户删除</h4><p>当你点击删除按钮的时候,会弹出确认框让你删除。然后网站通过ajax调用user_delete来实现真正的删除操作,其中,ajax代码位于static/js/myadmin/user_list.js </p><p>真正的删除函数是user_delete,下面是它的真面目</p><div class="rno-markdown-code"><div class="rno-markdown-code-toolbar"><div class="rno-markdown-code-toolbar-info"><div class="rno-markdown-code-toolbar-item is-type"><span class="is-m-hidden">代码语言:</span>txt</div></div><div class="rno-markdown-code-toolbar-opt"><div class="rno-markdown-code-toolbar-copy"><i class="icon-copy"></i><span class="is-m-hidden">复制</span></div></div></div><div class="developer-code-block"><pre class="prism-token token line-numbers language-txt"><code class="language-txt" style="margin-left:0">@ajax_required
@require_http_methods(["POST"])
def user_delete(request):
user_id = request.POST['user_id']
instance = User.objects.get(id=user_id)
instance.delete()
return JsonResponse({"code": 0, "msg": "success"})
首先获取到当前用户的实例,然后通过 instance.delete() 删除之。