Django实现管理课程

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chengqiuming/article/details/85839267

一 引入Django-braces

1 作用

包含了一些Django开发中常用的Mixin。

2 安装

(venv) E:\Django\mysite\mysite>pip install django-braces

二 编写视图类

# 引入ListView
from django.views.generic import TemplateView,ListView
from .models import Course
from braces.views import LoginRequiredMixin

class AboutView(TemplateView):
    template_name = "course/about.html"

# 继承ListView
class CourseListView(ListView):
    # 被类所使用的数据模型,能够得到数据表中的所有记录
    model = Course
    # 传入模板的变量名称
    context_object_name = "courses"
    # 模板文件
    template_name = 'course/course_list.html'

class UserMixin:
    def get_queryset(self):
        qs = super(UserMixin, self).get_queryset()
        return qs.filter(user=self.request.user)

# 增加一个继承类LoginRequiredMixin,用于判断是否登录
class UserCourseMixin(UserMixin, LoginRequiredMixin):
    model = Course
    # 声明用户登录URL
    login_url = "/account/login/"



class ManageCourseListView(UserCourseMixin, ListView):
    context_object_name = "courses"
    template_name = 'course/manage/manage_course_list.html'

三 编写前端模板

{% extends "article/base.html" %}
{% load staticfiles %}
{% block title %}管理课程{% endblock %}

{% block content %}
<div>
    <div class='text-right'><button type="button" class="btn btn-primary">添加课程</button></div>
    <table class="table table-hover" style="margin-top:10px">
        <tr>
            <td>序号</td>
            <td>课程标题</td>
            <td>发布日期</td>
            <td>操作</td>
        </tr>
        {% for course in courses %}
        <tr id={{ forloop.counter }}>
            <td>{{ forloop.counter }}</td>
            <td>{{ course.title }}</a></td>
            <td>{{ course.created|date:"Y-m-d" }}</td>
            <td>
                <a name="edit" href="#"><span class="glyphicon glyphicon-pencil"></span></a>
                <a class="delete" nane="delete" href="#" ><span class="glyphicon glyphicon-trash" style="margin-left:20px;"></span></a>
                <a href="#"><span class="glyphicon glyphicon-search" style="margin-left:20px;"></span></a>
            </td>
        </tr>
        {% endfor %}
    </table>
</div>
{% endblock %}

四 增加页面入口

<div class="bg-info">
    <div class="text-center" style="margin-top: 5px;">
        <p><h4>文章管理</h4></p>
        <p><a href="{% url 'article:article_column'%}">栏目管理</a></p>
        <p><a href="{% url 'article:article_post'%}">发布文章</a></p>
        <p><a href="{% url 'article:article_list' %}">文章列表</a></p>
        <p><a href="{% url 'article:article_tag' %}">文章标签</a></p>
    </div>
    <hr>
    <div class="text-center" style="margin-top: 5px;">
        <p><h4>图片管理</h4></p>
        <p><a href="{% url 'image:list_images' %}">图片管理</a></p>
    </div>
    <hr>
    <div class="text-center" style="margin-top: 5px;">
        <p><h4>课程管理</h4></p>
        <p><a href="{% url 'course:manage_course' %}">课程管理</a></p>
    </div>
</div>

五 配置URL

from django.conf.urls import url
from .views import AboutView,CourseListView,ManageCourseListView

urlpatterns = [
    url(r'about/$', AboutView.as_view(), name="about"),
    url(r'course-list/$', CourseListView.as_view(), name="course_list"),
    url(r'manage-course/$', ManageCourseListView.as_view(), name="manage_course"),
]

六 测试

猜你喜欢

转载自blog.csdn.net/chengqiuming/article/details/85839267