功能:
- 用户信息修改
- 全局搜索功能
- 我的课程
- 我的收藏(删除收藏)
- 我的消息
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})
我的收藏和我的消息类似我的课程的实现