基于django的视频点播网站开发-step11-后台用户管理功能

原文地址: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(&#39;password&#39;)
        user.set_password(password)
        user.save()
        return render(self.request, &#39;myadmin/user_add_success.html&#39;)
    return render(self.request, &#39;myadmin/user_add.html&#39;, {&#39;form&#39;: 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(&#39;user_list/&#39;, views.UserListView.as_view(), name=&#39;user_list&#39;),</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 = &#39;myadmin/user_list.html&#39;
context_object_name = &#39;user_list&#39;
paginate_by = 10
q = &#39;&#39;

def get_context_data(self, *, object_list=None, **kwargs):
    context = super(UserListView, self).get_context_data(**kwargs)
    paginator = context.get(&#39;paginator&#39;)
    page = context.get(&#39;page_obj&#39;)
    page_list = get_page_list(paginator, page)
    context[&#39;page_list&#39;] = page_list
    context[&#39;q&#39;] = self.q
    return context

def get_queryset(self):
    self.q = self.request.GET.get(&#34;q&#34;, &#34;&#34;)
    return User.objects.filter(username__contains=self.q).order_by(&#39;-date_joined&#39;)</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(&#39;user_edit/&lt;int:pk&gt;&#39;,views.UserEditView.as_view(), name=&#39;user_edit&#39;),</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 = &#39;myadmin/user_edit.html&#39;

def get_success_url(self):
    messages.success(self.request, &#34;保存成功&#34;)
    return reverse(&#39;myadmin:user_edit&#39;, kwargs={&#39;pk&#39;: self.kwargs[&#39;pk&#39;]})</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() 删除之。