在线教育项目(七)之个人中心和全局搜索功能

功能:

  1. 用户信息修改
  2. 全局搜索功能
  3. 我的课程
  4. 我的收藏(删除收藏)
  5. 我的消息

 1、个人信息修改部分还存在一些问题,教程修改邮箱需要激活邮箱,我的想法比较简单,和其他用户信息一起提交,校验邮箱是否重复(未实现)。

class InfoView(LoginRequiredMixin, View):
    def get(self, request):
        return render(request, 'usercenter-info.html')

    def post(self, request):
        user_info = UserInfoForm(request.POST, instance=request.user)
        if user_info.is_valid():
            user_info.save()
            return HttpResponse({"status": "success"}, content_type='application/json')
        else:
            return HttpResponse(json.dumps(user_info.errors), content_type='application/json')


class UploadImageView(View):
    def post(self, request):
        image_form = UploadImageForm(request.POST, request.FILES)
        if image_form.is_valid():
            image = image_form.cleaned_data['image']
            request.user.image = image
            request.user.save()
            return HttpResponse({"status": "success"}, content_type='application/json')
        else:
            return HttpResponse({"status": "fail"}, content_type='application/json')


class UpdatePwdView(View):
    def post(self, request):
        modify_forms = ModifyPwdForm(request.POST)
        if modify_forms.is_valid():
            pwd1 = request.POST.get('password1', '')
            pwd2 = request.POST.get('password2', '')
            if pwd1 != pwd2:
                return HttpResponse({"status": "fail", "msg": "密码不一致"}, content_type='application/json')
            user = request.user
            user.password = make_password(pwd1)
            user.save()
            return HttpResponse({"status": "success"}, content_type='application/json')
        else:
            return HttpResponse(json.dumps(modify_forms.errors), content_type='application/json')


class SendEmailView(LoginRequiredMixin, View):
    def get(self, request):
        email = request.GET.get('email', '')
        if UserProfile.objects.filter(email=email):
            return HttpResponse('{"email":"邮箱已存在"}', content_type='application/json')
        email_body, email_title, email_url = send_register_email(email, 'update_email')
        a = mark_safe("{0}<a href='{1}'>{2}</a>".format(email_body, email_url, email_url))
        return render(request, 'reset_link.html', locals())


class UpdateEmailView(LoginRequiredMixin, View):
    def post(self, request):
        email = request.POST.get('email', '')
        code = request.POST.get('code', '')
        flag = EmailVerifyRecord.objects.filter(email=email, code=code, send_type='update_email')
        if flag:
            user = request.user
            user.email = email
            user.save()
            return HttpResponse('{"status":"success"}', content_type='application/json')
        else:
            return HttpResponse('{"email":"验证码无效"}', content_type='application/json')

全局搜索

仔细观察发现,每个搜索框都在list页面,只需在每个list和主页写上逻辑即可,根据搜索的关键字的不同,跳转到不同的页面,并由关键字过滤出结果。

 以机构为例:

        # 搜索
        keyword = request.GET.get('keywords', '')
        if keyword:
            # i表示忽略大小写
            all_orgs = all_orgs.filter(
                Q(name__icontains=keyword) | Q(desc__icontains=keyword))

我的课程

class MyCourseView(LoginRequiredMixin, View):
    def get(self, request):
        user = request.user #当前登陆对象
        course_list = UserCourse.objects.filter(user=user)
        return render(request, 'usercenter-mycourse.html', {'course_list': course_list})

我的收藏和我的消息类似我的课程的实现

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值