美文网首页
web应用框架——Django实践项目(九)

web应用框架——Django实践项目(九)

作者: 思君_4cd3 | 来源:发表于2020-05-23 15:11 被阅读0次

    一.首页轮播图

    • 打开MXOline/urls.py文件,配置url
    from apps.operations.views import IndexView
    。。。
     path('',IndexView.as_view(), name='index'),
    
    • 打开apps/operations/views.py文件,创建视图
    from apps.operations.models import UserFavorite,CourseComments, Banner
    。。。
    class IndexView(View):
        def get(self, request, *args, **kwargs):
            """
            首页处理
            :param request:
            :param args:
            :param kwargs:
            :return:
            """
            # banner加载
            banners = Banner.objects.all().order_by("index")[:4]
    
             # 公开课加载 (除去banner之外的)
            courses = Course.objects.filter(is_banner=False)[:6]
    
             # 小banner
            banner_courses =  Course.objects.filter(is_banner=True)[:4]
            # 课程机构加载
            course_orgs = CourseOrg.objects.all()[:15]
            return render(request,'index.html',{
                "banners":banners,
                "courses":courses,
                "course_orgs":course_orgs,
    
             })
    
    • 打开templates/index.html文件



                     {% for banner in banners %}
                        <li>
                            <a href="{{ banner.url }}">
                                <img width="1200" height="478"
                                     src="{{ banner.image.url }}"/>
                            </a>
                        </li>
                     {% endfor %}
    
    • 进入xadmin后台管理,添加轮播图


    • 刷新页面



      点击图片可跳转到课程详情


    二.公开课程

    • 打开index.html文件



                        {% for course in courses %}
                        <div class="module1_{{ forloop.counter|add:2 }} box">
                            <a href="{% url 'course:detail' course.id %}">
                                <img width="233" height="190" src="{{ course.image.url }}"/>
                            </a>
                            <div class="des">
                                <a href="course-detail.html">
                                    <h2 title="{{ course.name }}">{{ course.name }}</h2>
                                </a>
                                <span class="fl">难度:<i class="key">{{ course.get_degree_display }}</i></span>
                                <span class="fr">学习人数:{{ course.students }}</span>
                            </div>
                            <div class="bottom">
                                <span class="fl" title="{{ course.course_org.name }}">{{ course.course_org.name }}</span>
                                <span class="star fr">{{ course.fav_nums }}</span>
                            </div>
                        </div>
                        {% endfor %}
    
    • 然后进入到xadmin管理页面,将课程信息中的是否为广告位调成否


    • 刷新页面


    公开课程轮播

    - 打开operations/views.py文件
     # 小banner
            banner_courses = Course.objects.filter(is_banner=True)[:4]
    。。。
     "banner_courses":banner_courses,
    
    • 打开index.html文件
     {% for banner_course in banner_courses %}
                                    <li>
                                        <a href="{% url 'course:detail' banner_course.id %}">
                                            <img width="470" height="300"
                                                 src="{{banner_course.image.url}}"/>
                                        </a>
                                    </li>
                                    {% endfor %}
    
    • 刷新网页


    三.课程推荐

    • 打开templates/index.html文件


     {% for org in course_orgs %}
                            <li class="five">
                                <a href="">
                                    <div class="company">
                                        <img width="184" height="100" src="{{ org.image.url }}"/>
                                        <div class="score">
                                            <div class="circle">
                                                <h2>{{ org.tag }}</h2>
                                            </div>
                                        </div>
                                    </div>
                                    <p><span class="key" title="{{ org.name }}">{{ org.name }}</span></p>
                                </a>
                            </li>
                            {% endfor %}
    
    • 刷新网页


    四.授课教师

    教师相关界面链接如下:https://pan.baidu.com/s/1G_T4_joFNpmxfq5x3DrHOA
    提取码:bkf1

    • 我们将这两个网页复制到templates中


    • 打开apps/organizations/urls.py
    from apps.organization.views import OrgView, AddAsk, TeacherListView, TeacherDeatailView
    ...
    # 讲师相关
        url(r'^teachers/$', TeacherListView.as_view(), name='teachers'),
        url(r'^teachers/(?P<teacher_id>\d+)$', TeacherDeatailView.as_view(), name='teacher_detail'),
    
    • 打开apps/organizations/views.py文件
    class TeacherListView(View):
        def get(self, request, *args, **kwargs):
            all_teachers = Teacher.objects.all()
            teacher_nums = all_teachers.count()
    
            hot_teachers = Teacher.objects.all().order_by("-click_nums")[:3]
    
            # 讲师分页
    
            try:
                page = request.GET.get('page', 1)
            except PageNotAnInteger:
                page = 1
            p = Paginator(all_teachers, per_page=5, request=request)  # 每页显示多少个
            teachers = p.page(page)
    
            return render(request, 'teachers-list.html', {
            "teachers": teachers,
        })
    
    
    class TeacherDeatailView(View):
        def get(self, request, teacher_id, *args, **kwargs):
            teacher = Teacher.objects.get(id=int(teacher_id))
            return render(request, 'teacher-detail.html', {
                "teacher": teacher
            })
    
    • 打开base.html文件


    • 开始继承模板



    • 修改内容信息
    • 打开teachers-list.html文件
                {% for teacher in teachers.object_list %}
                <dl class="des">
                        <dt>
                            <a href="#">
                                <div class="picpic">
                                    <img width="100" height="100" class="scrollLoading" src="{{ teacher.image.url }}"/>
                                </div>
                            </a>
                            <div class="btn">
                                <div class="fr btn2 bdsharebuttonbox"
                                     data-text="{{teacher.name}}"
                                     data-desc="我在#慕学网#发现了教师“{{teacher.name}}”,对学习中的小伙伴很有帮助,一起来看看吧。"
                                     data-comment="{{teacher.name}}金牌讲师,从业年限:{{teacher.work_years}}年"
                                     data-tag="share_1"
                                     >
                                    <span class="fl">分享</span>
                                    <a href="#" class="bds_more" data-cmd="more"></a>
                                </div>
                            </div>
                        </dt>
                        <dd>
                            <a href="teacher-detail.html">
                                <h1>{{teacher.name}}<span class="key picbig">金牌讲师</span></h1>
                            </a>
                            <ul class="cont">
                                <li>工作年限:<span>{{ teacher.work_years }}年</span></li>
                                <li>工作职位:<span>{{ teacher.work_position }}</span></li>
                                <li>就职公司:<span>{{ teacher.work_company }}&nbsp;</span></li>
                                <li>年龄:<span>{{ teacher.age}}岁</span></li>
                                <li>教学特点:<span>{{teacher.points}}</span></li>
                            </ul>
                        </dd>
                        <a class="buy buyservice" href="{% url 'org:teacher_detail' teacher.id %}"><br/>查看<br/>详情</a>
                    </dl>
                {% endfor %}
    

    • 刷新页面



      (此文章仅作为个人学习笔记使用,如有错误欢迎指正~)

      这些的代码可在我的GitHub上面查看:

    https://github.com/zhaoXiY/MXOline

    相关文章

      网友评论

          本文标题:web应用框架——Django实践项目(九)

          本文链接:https://www.haomeiwen.com/subject/pdnfahtx.html