django项目--使用自定义通用视图

一、新闻标签管理功能

1. 新闻标签列表

1.1 视图

class TagListView(MyPermissionRequiredMixin, MyListView):
    permission_required = ('myadmin.news_tag_list',)
    model = Tag
    page_header = '系统设置'
    page_option = '新闻标签管理'
    table_title = '新闻标签列表'

    fields = ['name', 'is_delete']

1.2 模板

myadmin/tag/tag_list.html

{% extends 'myadmin/base/obj_list.html' %}

{% block add_button %}
                        
{% endblock %}
{% block table_content %}
    
        #
        标签名
        是否可用
    
    {% for obj in page_obj %}
        
            {{ forloop.counter }}
            {{ obj.name }}
            {% if obj.is_delete %}否{% else %}是{% endif %}
        
    {% endfor %}
{% endblock %}

1.3路由

path('tags/', views.TagListView.as_view(), name='tag_list'),

2.新闻标签更新

2.1 视图

class TagUpdateView(MyPermissionRequiredMixin, UpdateView):
    permission_required = ('myadmin:news_tag_update', )
    model = Tag

    template_name = 'myadmin/tag/tag_detail.html'
    page_header = '系统设置'
    page_option = '新闻标签管理'
    table_title = '新闻标签修改'
    fields = ['name', 'is_delete']

2.2 模板

myadmin/tag/tag_detail.html

{% extends 'myadmin/base/obj_detail.html' %}

{% block update_url %}{% url 'myadmin:news_tag_update' form.instance.id %}{% endblock %}
{% block add_url %}{% url 'myadmin:news_tag_add' %}{% endblock %}

2.3 路由

path('tag//', views.TagUpdateView.as_view(), name='news_tag_update'),

3.添加新闻标签

3.1 视图

class TagAddView(MyPermissionRequiredMixin, AddView):
    permission_required = ('myadmin.news_tag_add')
    model = Tag
    page_header = '系统设置'
    page_option = '新闻标签管理'
    table_title = '新闻标签添加'
    fields = ['name', 'is_delete']

3.2 模板

同更新模板

3.3 路由

path('tag/', views.TagAddView.as_view(), name='add_tag')

二、热门新闻管理功能

1.热门新闻列表

1.1 视图

class HotNewsListView(MyPermissionRequiredMixin, MyListView):
    permission_required = ('myadmin.hotnews_list',)
    model = HotNews

    page_header = '新闻管理'
    page_option = '热门新闻管理'
    table_title = '热门新闻列表'
    fields = ['news__title', 'news__tag__name', 'priority', 'is_delete']

    is_paginate = True
    per_page = 10

    def get_queryset(self):
        queryset = super().get_queryset()
        return queryset.select_related('news__tag').order_by('-priority')

1.2 模板


{% extends 'myadmin/base/obj_list.html' %}
{% block search_form %}
{% endblock %}
{% block add_button %}
    
{% endblock %}
{% block table_content %}
    
        #
        标题
        类别
        优先级
        逻辑删除
    
    {% for obj in page_obj %}
        
            {{ forloop.counter }}
            {{ obj.news.title }}
            {{ obj.news.tag.name }}
            {{ obj.priority }}
            {% if obj.is_delete %}是{% else %}否{% endif %}
        
    {% endfor %}
{% endblock %}

1.3 路由

    path('hotnewses/', views.HotNewsListView.as_view(), name='hotnews_list'),

2.热门新闻更新

2.1 视图

class HotNewsUpdateView(MyPermissionRequiredMixin, UpdateView):
    permission_required = ('myadmin.hotnews_update',)
    model = HotNews

    page_header = '新闻管理'
    page_option = '热门新闻管理'
    table_title = '热门新闻更新'
    fields = ['priority', 'is_delete']

2.2 模板


{% extends 'myadmin/base/obj_detail.html' %}

{% block update_url %}{% url 'myadmin:hotnews_update' form.instance.id %}{% endblock %}
{% block add_url %}{% url 'myadmin:hotnews_add' %}{% endblock %}

2.3 路由

path('hotnews//', views.HotNewsUpdateView.as_view(), name='hotnews_update'),

3.热门新闻添加

3.1 视图

class HotNewsAddView(MyPermissionRequiredMixin, AddView):
    permission_required = ('myadmin.hotnews_add',)
    model = HotNews

    page_header = '新闻管理'
    page_option = '热门新闻管理'
    table_title = '热门新闻添加'
    fields = ['news', 'priority']

3.2 模板

同更新模板

3.3 路由

    path('hotnews/', views.HotNewsAddView.as_view(), name='hotnews_add')

三、轮播图管理功能

1. 轮播图列表

1.1 视图

class NewsBannerListView(MyPermissionRequiredMixin, MyListView):
    permission_required = ('myadmin.banner_list',)
    model = Banner
    page_header = '新闻管理'
    page_option = '轮播图管理'
    table_title = '轮播图列表'

    fields = ['image_url', 'priority', 'news__title', 'news__tag__name', 'is_delete']

    def get_queryset(self):
        queryset = super().get_queryset()
        return queryset.select_related('news__tag').order_by('-priority')

1.2 模板


{% extends 'myadmin/base/obj_list.html' %}
{% block add_button %}
    
{% endblock %}

{% block table_content %}
    
        #
        图片
        新闻标题
        新闻类别
        优先级
        逻辑删除
    
    {% for obj in page_obj %}
        
            {{ forloop.counter }}
            
            {{ obj.news.title }}
            {{ obj.news.tag }}
            {{ obj.priority }}
            {% if obj.is_delete %}是{% else %}否{% endif %}
        
    {% endfor %}
{% endblock %}

1.3 路由

    path('banners/', views.NewsBannerListView.as_view(), name='banner_list'),

2. 录播图更新

2.1 视图

class NewsBannerUpdateView(MyPermissionRequiredMixin, UpdateView):
    permission_required = ('myadmin.news_banner_update', )
    model = Banner
    page_header = '新闻管理'
    page_option = '轮播图管理'
    table_title = '轮播图更新'

    fields = ['image_url', 'priority', 'news', 'is_delete']

注意修改模型中的image_url字段的类型与新闻模型中的字段一致。

2.2 模板

{% extends 'myadmin/base/obj_detail.html' %}

{% block update_url %}{% url 'myadmin:banner_update' form.instance.id %}{% endblock %}
{% block add_url %}{% url 'myadmin:banner_add' %}{% endblock %}

2.3 路由

    path('banner//', views.NewsBannerUpdateView.as_view(), name='banner_update'),

3. 轮播图添加

3.1 视图

class NewsBannerAddView(MyPermissionRequiredMixin, AddView):
    permission_required = ('myadmin.news_banner_add', )
    model = Banner
    page_header = '新闻管理'
    page_option = '轮播图管理'
    table_title = '轮播图添加'

    fields = ['image_url', 'priority', 'news', 'is_delete']

3.2模板

模板同更新视图

3.3 路由

    path('banner/', views.NewsBannerAddView.as_view(), name='banner_add'),

四、文档管理

1.文档列表

1.1 视图

class DocListView(MyPermissionRequiredMixin, MyListView):
    permission_required = ('myadmin.doc_list', )
    model = Doc
    page_header = '文档管理'
    page_option = '文档管理'
    table_title = '文档列表'

    fields = ['title', 'file_name', 'desc', 'file_url', 'image_url', 'author__username', 'is_delete']

    def get_queryset(self):
        queryset = super().get_queryset()
        return queryset.select_related('author')

1.2 模板

{% extends 'admin/base/obj_list.html' %}
{% block add_button %}
     
{% endblock %}
{% block table_content %}
    
        #
        标题
        文件名
        简介
        图片
        作者
        文件地址
        逻辑删除
    
    {% for obj in page_obj %}
        
            {{ forloop.counter }}
            {{ obj.title }}
            {{ obj.file_name }}
            {{ obj.desc }}
            
            {{ obj.author.username }}
            {{ obj.file_url }}
            {% if obj.is_delete %}是{% else %}否{% endif %}
        
    {% endfor %}
{% endblock %}

1.3 路由

    path('docs/', views.DocListView.as_view(), name='doc_list'),

2.文档更新

2.1 视图

class DocUpdateView(MyPermissionRequiredMixin, UpdateView):
    permission_required = ('myadmin.doc_update',)
    model = Doc
    page_header = '文档管理'
    page_option = '文档管理'
    table_title = '文档更新'

    fields = ['title', 'file_name', 'desc', 'file_url', 'image_url', 'author', 'is_delete']

2.2 模板

{% extends 'admin/base/obj_detail.html' %}

{% block update_url %}
    {% url 'myadmin:doc_update' form.instance.id %}
{% endblock %}

{% block add_url %}
    {% urlmyadmin:n:doc_add' %}
{% endblock %}

2.3 路由

path('doc//', views.DocUpdateView.as_view(), name='doc_update'),

3.文档添加

3.1 视图

class DocAddView(MyPermissionRequiredMixin, AddView):
    permission_required = ('myadmin.doc_add', )
    model = Doc
    page_header = '文档管理'
    page_option = '文档管理'
    table_title = '文档添加'

    fields = ['title', 'file_name', 'desc', 'file_url', 'image_url', 'is_delete']

    def save(self, form):
        instance = form.save(commit=False)
        instance.author = self.request.user
        instance.save()
        # TODO 文档还没写

3.2 模板

同文档更新

3.3 路由

    path('doc/', views.DocAddView.as_view(), name='doc_add'),

五、课程管理

1. 课程列表

1.1 视图

class CourseListView(MyPermissionRequiredMixin, MyListView):
    permission_required = ('myadmin.course_list', )
    model = Course
    page_header = '在线课堂'
    page_option = '课程管理'
    table_title = '课程列表'

    fields = ['title', 'category__name', 'teacher__name', 'profile', 'outline',  'duration', 'cover_url', 'is_delete']


1.2 模板

{% extends 'admin/base/obj_list.html' %}
{% block search_form %}
{% endblock %}
{% block add_button %}
     
{% endblock %}
{% block table_content %}
    
        #
        标题
        类型
        老师
        简介
        大纲
        时长
        封面
        逻辑删除
    
    {% for obj in page_obj %}
        
            {{ forloop.counter }}
            {{ obj.title }}
            {{ obj.category.name }}
            {{ obj.teacher.name }}
            {{ obj.profile|default:'无' }}
            {{ obj.outline|default:'无' }}
            {{ obj.duration }}
            
            {% if obj.is_delete %}是{% else %}否{% endif %}
        
    {% endfor %}
{% endblock %}

1.3 路由

    path('courses/', views.CourseListView.as_view(), name='course_list'),

2. 课程更新

2.1 视图

class CourseUpdateView(MyPermissionRequiredMixin, UpdateView):
    permission_required = ('myadmin.course_update', )
    model = Course
    page_header = '在线课堂'
    page_option = '课程管理'
    table_title = '课程更新'

    fields = ['title', 'category', 'teacher', 'profile', 'outline', 'duration', 'cover_url', 'video_url']

2.2 模板

{% extends 'admin/base/obj_detail.html' %}

{% block update_url %}
    {% url 'myadmin:course_update' form.instance.id %}
{% endblock %}
{% block add_url %}
    {% urlmyadmin:n:course_add' %}
{% endblock %}

2.3 路由

path('course//', views.CourseUpdateView.as_view(), name='course_update'),

3. 课程添加

3.1 视图


class CourseAddView(MyPermissionRequiredMixin, AddView):

    permission_required = ('myadmin.course_add', )
    model = Course
    page_header = '在线课堂'
    page_option = '课程管理'
    table_title = '课程添加'

    fields = ['title', 'category', 'teacher', 'profile', 'outline', 'duration', 'cover_url', 'video_url']

3.2 模板

同更新视图

3.3 路由

    path('course/', views.CourseAddView.as_view(), name='course_add'),

你可能感兴趣的:(django项目--使用自定义通用视图)