您现在的位置是: 网站首页 >Django >Django2.0.8+xadmin2实现在线学习网站 Django

【Django在线教育平台】19.个人中心显示我的课程、收藏、消息页面

admin2019年6月4日 14:19 Django | Html | JQuery 298人已围观

Django2.0.8+xadmin2实现在线学习网站简介 Django2.0.8+xadmin2实现在线学习网站,课程、讲师、机构、用户收藏功能。GitHub地址:https://github.com/xyliurui/OnlineLearningPlatform ;Django版本:2.0.8

## 我的课程页面 ### 显示我的课程视图 ```python # 我的课程 class MyCourseView(LoginRequiredMixin, View): login_url = '/login/' redirect_field_name = 'next' def get(self, request): all_user_course = UserCourse.objects.filter(user=request.user) return render(request, 'usercenter-course.html', locals()) ``` ### 我的课程url ```python from .views import UserCenterInfoView, UserModifypwdView, UserImageUploadView, ModifyEmailSendCodeView, SaveEmailModifyView, MyCourseView app_name = 'users' urlpatterns = [ # 用户相关url path('info/', UserCenterInfoView.as_view(), name='user_info'), # 用户信息 path('password/modify/', UserModifypwdView.as_view(), name='user_modify_pwd'), # 修改密码 path('image/upload/', UserImageUploadView.as_view(), name='user_image_upload'), # 用户上传头像 path('verify_code/send/', ModifyEmailSendCodeView.as_view(), name='user_send_verify_code'), # 用户修改邮箱发送验证码 path('email_modify/save/', SaveEmailModifyView.as_view(), name='user_save_email_modify'), # 用户保存邮箱修改 path('my_course/', MyCourseView.as_view(), name='my_course'), # 我的课程 ] ``` ### 我的课程模板 ```html {% for user_course in all_user_course %} {% with course=user_course.course %} <a href="{% url 'course:course_detail' course.id %}">***</a> {% endwith %} {% endfor %} ``` ![BLOG_20190604_142119_68](/media/blog/images/2019/06/BLOG_20190604_142119_68.png "博客图集BLOG_20190604_142119_68.png") ## 我的收藏页面 ### 显示我的收藏视图 ```python # 我的收藏 class MyFavoriteView(LoginRequiredMixin, View): login_url = '/login/' redirect_field_name = 'next' def get(self, request): # 获取所有用户收藏的课程 all_user_fav_course = UserFavorite.objects.filter(user=request.user, fav_type=1) all_fav_course = [Course.objects.get(id=user_fav_course.fav_id) for user_fav_course in all_user_fav_course] # 获取所有用户收藏的讲师 all_user_fav_teacher = UserFavorite.objects.filter(user=request.user, fav_type=3) all_fav_teacher = [Teacher.objects.get(id=user_fav_teacher.fav_id) for user_fav_teacher in all_user_fav_teacher] # 收藏的机构 all_user_fav_org = UserFavorite.objects.filter(user=request.user, fav_type=2) all_fav_org = [CourseOrg.objects.get(id=user_fav_org.fav_id) for user_fav_org in all_user_fav_org] return render(request, 'usercenter-fav.html', locals()) ``` ### 我的收藏url ```python from .views import UserCenterInfoView, UserModifypwdView, UserImageUploadView, ModifyEmailSendCodeView, SaveEmailModifyView, MyCourseView, MyFavoriteView app_name = 'users' urlpatterns = [ # 用户相关url path('info/', UserCenterInfoView.as_view(), name='user_info'), # 用户信息 path('password/modify/', UserModifypwdView.as_view(), name='user_modify_pwd'), # 修改密码 path('image/upload/', UserImageUploadView.as_view(), name='user_image_upload'), # 用户上传头像 path('verify_code/send/', ModifyEmailSendCodeView.as_view(), name='user_send_verify_code'), # 用户修改邮箱发送验证码 path('email_modify/save/', SaveEmailModifyView.as_view(), name='user_save_email_modify'), # 用户保存邮箱修改 path('my_course/', MyCourseView.as_view(), name='my_course'), # 我的课程 path('my_favorite/', MyFavoriteView.as_view(), name='my_favorite'), # 我的收藏 ] ``` ### 我的收藏模板 ```html {% for course in all_fav_course %} {% endfor %} ``` ![BLOG_20190604_142110_35](/media/blog/images/2019/06/BLOG_20190604_142110_35.png "博客图集BLOG_20190604_142110_35.png") ## 我的消息页面 ### 用户消息显示视图和分页 ```python class MyMessageView(LoginRequiredMixin, View): login_url = '/login/' redirect_field_name = 'next' def get(self, request): all_user_msg = UserMessage.objects.all().filter(user=request.user.id) # 分页 try: page = request.GET.get('page', 1) except PageNotAnInteger: page = 1 # 这里指从all_course中取8个出来,每页显示8个 p = Paginator(all_user_msg, 8, request=request) all_user_msg = p.page(page) return render(request, 'usercenter-msg.html', locals()) ``` ### 用户消息url ```python from .views import UserCenterInfoView, UserModifypwdView, UserImageUploadView, ModifyEmailSendCodeView, SaveEmailModifyView, MyCourseView, MyFavoriteView, MyMessageView app_name = 'users' urlpatterns = [ # 用户相关url path('info/', UserCenterInfoView.as_view(), name='user_info'), # 用户信息 path('password/modify/', UserModifypwdView.as_view(), name='user_modify_pwd'), # 修改密码 path('image/upload/', UserImageUploadView.as_view(), name='user_image_upload'), # 用户上传头像 path('verify_code/send/', ModifyEmailSendCodeView.as_view(), name='user_send_verify_code'), # 用户修改邮箱发送验证码 path('email_modify/save/', SaveEmailModifyView.as_view(), name='user_save_email_modify'), # 用户保存邮箱修改 path('my_course/', MyCourseView.as_view(), name='my_course'), # 我的课程 path('my_favorite/', MyFavoriteView.as_view(), name='my_favorite'), # 我的收藏 path('my_message/', MyMessageView.as_view(), name='my_message'), # 我的消息 ] ``` ### 注册完成后发送消息给用户 ```python # 用户注册 class RegisterView(View): def get(self, request): # print(request.build_absolute_uri()) # 地址为: http://127.0.0.1:8000/register/ register_form = RegisterForm() # 图片验证码 # hashkey验证码生成的秘钥,image_url验证码的图片地址 hashkey = CaptchaStore.generate_key() image_url = captcha_image_url(hashkey) return render(request, 'register.html', { 'register_form': register_form, 'hashkey': hashkey, 'image_url': image_url, }) def post(self, request): register_form = RegisterForm(request.POST) # 图片验证码 hashkey = CaptchaStore.generate_key() image_url = captcha_image_url(hashkey) if register_form.is_valid(): user_name = request.POST.get("email", "") pass_word = request.POST.get("password", "") # 用户不为空字符串,且用户 if user_name.strip() != '' and not UserProfile.objects.filter(email=user_name): # 实例化一个user_profile对象,将前台值存入 user_profile = UserProfile() user_profile.username = user_name user_profile.email = user_name # 加密password进行保存 user_profile.password = make_password(pass_word) # 默认激活状态True,需要改为False user_profile.is_active = False user_profile.save() # 写入欢迎注册消息到用户消息 user_message = UserMessage() user_message.user = user_profile.id user_message.message = '欢迎注册在线学习平台' user_message.save() # 发送注册激活邮件 send_register_email(request_uri=request.build_absolute_uri(), email=user_name, send_type='register') return render(request, 'login.html') else: return render(request, 'register.html', { 'register_form': register_form, 'msg': '邮箱已使用!', 'hashkey': hashkey, 'image_url': image_url, }) return render(request, 'register.html', { 'register_form': register_form, 'hashkey': hashkey, 'image_url': image_url, }) ``` ### 用户消息模板 ```html {% for user_msg in all_user_msg.object_list %} <p style="color: #BEBEBE"><i class="fa fa-clock-o"></i> {{ user_msg.add_time }}</p> <p class="padding-bottom-20 padding-left-15">{{ user_msg.message }}</p> {% endfor %} <ul class="container pagination padding-top-30"> {% if all_user_msg.has_previous %} <li><a href="?{{ all_user_msg.previous_page_number.querystring }}" aria-label="Previous"> <i class="fa fa-angle-left"></i> </a></li> {% endif %} {% for page in all_user_msg.pages %} {% if page %} {% ifequal page all_user_msg.number %} <li><a class="active" href="?{{ page.querystring }}">{{ page }}</a></li> {% else %} <li><a href="?{{ page.querystring }}">{{ page }}</a></li> {% endifequal %} {% else %} <li><a href="">...</a></li> {% endif %} {% endfor %} {% if all_user_msg.has_next %} <li><a href="?{{ all_user_msg.next_page_number.querystring }}" aria-label="Next"> <i class="fa fa-angle-right"></i> </a></li> {% endif %} </ul> ``` ![BLOG_20190604_142056_61](/media/blog/images/2019/06/BLOG_20190604_142056_61.png "博客图集BLOG_20190604_142056_61.png") ### 页首显示用户消息数量 模型中获取用户消息数量 ```python class UserProfile(AbstractUser): # 自定义的性别选择规则 GENDER_CHOICES = ( ('male', '男'), ('female', '女') ) nick_name = models.CharField(max_length=50, default='', verbose_name='昵称') birthday = models.DateField(null=True, blank=True, verbose_name='生日') gender = models.CharField(max_length=10, choices=GENDER_CHOICES, default='male', verbose_name='性别') address = models.CharField(max_length=100, default='', verbose_name='地址') mobile = models.CharField(max_length=11, null=True, blank=True, verbose_name='电话') image = models.ImageField(upload_to='image/%Y/%m', default='image/default.jpg', blank=True, null=True, max_length=100, verbose_name='头像') def get_unread_nums(self): # 获取用户未读消息,import需要放在这儿,如果放在头部,会产生循环import from operation.models import UserMessage return UserMessage.objects.filter(user=self.id, has_read=False).count() # Meta信息,即后台栏目名 class Meta: verbose_name_plural = verbose_name = '用户信息' def __str__(self): return self.username ``` 模型中显示数量 ```html <li><a href="{% url 'users:user_info' %}"><b>{{ user.nick_name }}({{ user.username }})</b> </a></li> <li><a href="{% url 'usercenter:my_message' %}"><i class="fa fa-bullhorn"></i> {{ user.get_unread_nums }}</a></li> ```

很赞哦! (0)

文章交流

  • emoji
0人参与,0条评论

当前用户

未登录,点击   登录

站点信息

  • 建站时间:网站已运行657天
  • 系统信息:Linux
  • 后台程序:Python: 3.6.6
  • 网站框架:Django: 2.2.12
  • 文章统计:235 篇
  • 文章评论:40 条
  • 腾讯分析网站概况-腾讯分析
  • 百度统计网站概况-百度统计
  • 公众号:微信扫描二维码,关注我们
  • QQ群:QQ加群,下载网站的学习源码
返回
顶部
标题 换行 登录
网站