Realization of the Functions of Teaching Institutions in Online Education Projects


  1. List of Teaching Institutions (Pageable, Selectable by Category, Area, Sorted by Number of Students and Courses)
  2. Institutional Details Page (Collectable Institutions, What Courses, Lecturers, and Institutional Introduction)
  3. Consultable courses (using ModelForm)

Corresponding url

urlpatterns = [
    url(r'^list/', OrgView.as_view(), name='org_list'),
    url(r'^add_ask/', AskView.as_view(), name='add_ask'),
    url(r'^home/(?P<org_id>\d+)/$', OrgHomeView.as_view(), name='org_home'),#Home page of organization
    url(r'^course/(?P<org_id>\d+)/$', OrgCourseView.as_view(), name='org_course'),#Institutional Curriculum
    url(r'^desc/(?P<org_id>\d+)/$', OrgDescView.as_view(), name='org_desc'),
    url(r'^org_teacher/(?P<org_id>\d+)/$', OrgTeacherView.as_view(), name='org_teacher'),

    # Collection
    url(r'^add_fav/', AddFavView.as_view(), name='add_fav'),

Implementation of mechanism list function

After implementation, return to the front-end page and fill it with template language.

class OrgView(View):
    //Curriculum organization

    def get(self, request):
        # All institutions
        all_orgs = CourseOrg.objects.all()
        org_nums = all_orgs.count()
        # All cities
        all_citys = CityDict.objects.all()

        # search
        keyword = request.GET.get('keywords', '')
        if keyword:
            # i means ignoring case
            all_orgs = all_orgs.filter(
                Q(name__icontains=keyword) | Q(desc__icontains=keyword))

        # Class selection
        category = request.GET.get('ct', '')
        if category:
            all_orgs = all_orgs.filter(category=category)

        # City screening
        city_id = request.GET.get('city', '')
        if city_id:
            all_orgs = all_orgs.filter(city_id=int(city_id))

        # Top agencies ranked by click number
        hot_orgs = all_orgs.order_by('-click_nums')[:3]

        # Ranking by number of students or courses in an institution
        sort = request.GET.get('sort', '')
        if sort == 'students':
            all_orgs = all_orgs.order_by('-students')
        elif sort == 'courses':
            all_orgs = all_orgs.order_by('-course_nums')

        # Paging, two per page
            page = request.GET.get('page', 1)
        except PageNotAnInteger:
            page = 1
        p = Paginator(all_orgs, 2, request=request)
        orgs =
        return render(request, 'org-list.html',
                          'all_orgs': orgs,
                          'all_citys': all_citys,
                          'org_nums': org_nums,
                          'hot_orgs': hot_orgs,
                          'category': category,
                          # Returns sort and city_id for judging the selected state and adding active attributes
                          'sort': sort,
                          'city_id': city_id,

Paging implementation:

Paging uses the django pure_pagination library, which can be downloaded from github

Home page of organization

On the list of institutions, click an organization, send a get request to the background, bring the id of the course, and get the courses, lecturers and lecturers.

class OrgHomeView(View):
    def get(self, request, org_id):
        current_page = 'home'
        # What courses, teachers and lecturers are there in this institution?
        course_org = CourseOrg.objects.get(id=int(org_id))
        has_fav = False
        #Judging whether it has been collected
        if request.user.is_authenticated:
            if UserFavorite.objects.filter(user=request.user,, fav_type=2):
                has_fav = True
        # Reverse Search for Courses and Lecturers
        all_courses = course_org.course_set.all()[:4]
        all_teacher = course_org.teacher_set.all()[:2]
        return render(request, 'org-detail-home.html', {'course_org': course_org,
                                                        'all_courses': all_courses,
                                                        'all_teacher': all_teacher,
                                                        'current_page': current_page,
                                                        'has_fav': has_fav})

Advisory function

Send ajax requests

        //Bind Click Events to the Immediate Consultation Button
        $('#jsStayBtn').click(function () {
                cache: false,
                url: "{% url 'org:add_ask' %}",
                type: "post",
                data: $('#jsStayForm').serialize(), // serializes the value of the form into a string
                async: true,
                success: function (data) {
                    if (data.status == 'success') {
                        alert('Submit successfully');
                    } else if (data.status == 'fail') {
                        $('#JsCompanyTips'. HTML (data. msg); //Delete error messages

Background code:

class AskView(View):
    //Users add consulting, only post requests

    def post(self, request):
        use_post = UserAskForm(request.POST)  # Using ModelForm, instantiate
        if use_post.is_valid():
    # When commit is True, the submitted data can be stored directly in the database.
            return HttpResponse('{"status":"success"}', content_type='application/json')
            return HttpResponse('{"status":"fail","msg":"Add error"}', content_type='application/json')

Institutional curriculum, introduction and lecturer function realization:

According to the mechanism id, the corresponding data are obtained separately

class OrgCourseView(View): 
    def get(self, request, org_id):

        current_page = 'course'
        course_org = CourseOrg.objects.get(id=int(org_id))
        # Reverse Search Course
        has_fav = False
        if request.user.is_authenticated:
            if UserFavorite.objects.filter(user=request.user,, fav_type=2):
                has_fav = True
        all_courses = course_org.course_set.all()[:4]
        return render(request, 'org-detail-course.html', {'course_org': course_org,
                                                          'all_courses': all_courses,
                                                          'current_page': current_page,
                                                          'has_fav': has_fav})

Collection function realization:

Collection needs to determine whether it has been collected, and if it has been collected, it will be cancelled. Collections can be divided into three categories: teachers, institutions or courses.

class AddFavView(View):
    def post(self, request):
        id = request.POST.get('fav_id', 0)
        type = request.POST.get('fav_type', 0)
        if not request.user.is_authenticated:
            # Not logged in
            return HttpResponse('{"status":"fail","msg":"User not logged in"}', content_type='application/json')
        exist_record = UserFavorite.objects.filter(user=request.user, fav_id=int(id), fav_type=int(type))
        if exist_record:
            # Users have already collected, click again to cancel the collection
            if int(type) == 1:
                course = Course.objects.get(id=int(id))
                course.fav_nums -= 1
                if course.fav_nums < 0:
                    course.fav_nums = 0

            elif int(type) == 2:
                teacher_obj = Teacher.objects.get(id=int(id))
                teacher_obj.fav_nums -= 1
                if teacher_obj.fav_nums < 0:
                    teacher_obj.fav_nums = 0

            elif int(type) == 3:
                course = Course.objects.get(id=int(id))
                course.fav_nums -= 1
                if course.fav_nums < 0:
                    course.fav_nums = 0

            return HttpResponse({"status": "success", "msg": "Collection"}, content_type='application/json')
            user_fav = UserFavorite()
            if int(type) > 0 and int(id) > 0:
                user_fav.fav_id = int(id)
                user_fav.fav_type = int(type)
                user_fav.user = request.user

                if int(type) == 1:
                    course = Course.objects.get(id=int(id))
                    course.fav_nums += 1
                elif int(type) == 2:
                    org = CourseOrg.objects.get(id=int(id))
                    org.fav_nums += 1
                elif int(type) == 3:
                    teacher = Teacher.objects.get(id=int(id))
                    teacher.fav_nums += 1
                return HttpResponse({"status": "success", "msg": "Have been collected"}, content_type='application/json')
                return HttpResponse({"status": "fail", "msg": "Collection error"}, content_type='application/json')


Tags: JSON Django github Database

Posted on Mon, 07 Oct 2019 07:03:18 -0700 by efron