网站开发资金,新闻app开发,安阳县事业单位招聘2021,铝单板设计师招聘网接着上期代码框架#xff0c;开发第5个功能#xff0c;兑换物品管理#xff0c;再增加一个学习兑换物品表#xff0c;主要用来维护兑换物品#xff0c;所需积分#xff0c;物品状态等信息#xff0c;还有一个积分流水表#xff0c;完成任务奖励积分#xff0c;兑换物品…接着上期代码框架开发第5个功能兑换物品管理再增加一个学习兑换物品表主要用来维护兑换物品所需积分物品状态等信息还有一个积分流水表完成任务奖励积分兑换物品消耗积分。
要想激励一个人的学习动力最好能有实际的奖励完成任务后系统应给予一定的奖励如积分或实际物品等。
目前系统考虑使用积分奖励然后这些积分积累多了就可以兑换实际物品这些奖励可以激励用户持续学习。
第一步编写第5个功能-兑换物品管理
1编辑模型文件
./mysite/study_system/models.py:
# 更多代码内容请关注weixin公众号: PandaCode辉
2编辑urls配置文件 ./mysite/study_system/urls.py
# 更多代码内容请关注weixin公众号: PandaCode辉
3编辑视图文件 ./mysite/study_system/views.py
def getExchangeItemList(request):方法名称: 获取兑换物品列表作 者: PandaCode辉weixin公众号: PandaCode辉创建时间: 2023-10-10# 响应容器rsp_dict {}# 获取当前用户名username request.session.get(username)# 根据用户名获取用户对象cur_user StudyUser.objects.get(usernameusername)print(根据用户名查询用户对象: str(cur_user))user_list [cur_user]# 如果当前用户是3-学生则查找对应辅导员用户if cur_user.role 3:parent_id cur_user.parent_id# 根据用户ID获取用户对象parent_user StudyUser.objects.get(user_idparent_id)print(根据用户ID获取用户对象: str(parent_user))user_list [cur_user, parent_user]# 获取待完成任务列表限制发布人data_list StudyExchangeItem.objects.filter(created_by__inuser_list).order_by(-pk)rsp_dict[data_list] data_listcontext_object_name exchange_item_listtemplate_name study_system/home.htmlrsp_dict[html_file] study_system/item/exchangeItemList.htmlrsp_dict[context_object_name] context_object_namereturn render(request, template_name, rsp_dict)def toNewExchangeItem(request):方法名称: 跳转到新增兑换物品视图作 者: PandaCode辉weixin公众号: PandaCode辉创建时间: 2023-10-10rsp_dict {}rsp_dict[pageTitle] 新增兑换物品rsp_dict[html_file] study_system/item/addExchangeItem.htmlreturn render(request, study_system/home.html, rsp_dict)def addExchangeItem(request):方法名称: ajax请求, 表单视图新增兑换物品作 者: PandaCode辉weixin公众号: PandaCode辉创建时间: 2023-10-10# 初始化响应容器rsp_dict {result: error, errorMsg: 系统错误}# 是否ajax请求if request.is_ajax():try:rest request.POSTitem_name rest[itemName]item_points int(rest[itemPoints])item_description rest[itemDescription]item_status rest[itemStatus]# 获取当前用户名username request.session.get(username)# 根据用户名获取用户对象cur_user StudyUser.objects.get(usernameusername)print(根据用户名查询用户对象: str(cur_user))# 创建者ID使用 StudyUser 对象赋值created_by cur_user# 今天# UTC格式当前时区时间t time.localtime()work_date time.strftime(%Y-%m-%d %H:%M:%S, t)print(当前日期时间: str(work_date))# 创建对象并保存到数据库studyExchangeItem StudyExchangeItem(item_nameitem_name, item_pointsitem_points,item_descriptionitem_description, item_statusitem_status,created_timework_date, update_timework_date,created_bycreated_by)# 保存到数据库是否成功studyExchangeItem.save()rsp_dict[result] successexcept Exception as e:rsp_dict[errorMsg] 新增兑换物品保存到数据库失败.# 成功与否都返回json数据格式return JsonResponse(rsp_dict) 兑换物品视图更新物品状态同时添加消耗积分流水。
# 更多代码内容请关注weixin公众号: PandaCode辉 4编辑页面模板代码
4.1. 兑换物品列表页面 ./mysite/study_system/templates/study_system/item/exchangeItemList.html
# 更多代码内容请关注weixin公众号: PandaCode辉 4.2. 新增兑换物品页面 ./mysite/study_system/templates/study_system/item/addExchangeItem.html
# 更多代码内容请关注weixin公众号: PandaCode辉
第二步运行测试-兑换物品管理功能
1登录用户后点击查看兑换物品列表页面 点击可兑换物品如果用户积分充足则兑换成功更新物品状态。 -------------------------------------------------end -------------------------------------------------