接著上期代碼框架,開發(fā)第4個功能,定時任務(wù)管理,再增加一個學(xué)習(xí)定時任務(wù)表,主要用來設(shè)置周期重復(fù)性的學(xué)習(xí)任務(wù),定時周期,定時時間,任務(wù)標(biāo)題和內(nèi)容,預(yù)計完成天數(shù),獎勵積分和任務(wù)狀態(tài)等信息。
現(xiàn)實中學(xué)習(xí)一門課程或一項技能知識,需要很長時間的學(xué)習(xí)積累才能學(xué)會,不像小說世界,游戲世界等虛擬世界,可以快速學(xué)會一項技能知識。
所以學(xué)習(xí)任務(wù)系統(tǒng)最重要的是周期重復(fù)性的學(xué)習(xí)任務(wù),最好是每天做一次的任務(wù),一點一點的累積,堅持幾個月或幾年,才能完成掌握一門課程或一項技能知識。
第一步:編寫第4個功能-定時任務(wù)管理
1,編輯模型文件:
./mysite/study_system/models.py:
# 更多代碼內(nèi)容請關(guān)注weixin公眾號: PandaCode輝
2,編輯urls配置文件:
./mysite/study_system/urls.py
# 更多代碼內(nèi)容請關(guān)注weixin公眾號: PandaCode輝
3,編輯視圖文件:
./mysite/study_system/views.py
def getDayTaskList(request):
'''
@方法名稱: 獲取定時任務(wù)列表
@作 者: PandaCode輝
@weixin公眾號: PandaCode輝
@創(chuàng)建時間: 2023-10-10
'''
# 響應(yīng)容器
rsp_dict = {}
# 獲取當(dāng)前用戶名
username = request.session.get('username')
# 根據(jù)用戶名獲取用戶對象
cur_user = StudyUser.objects.get(username=username)
print('根據(jù)用戶名查詢用戶對象:' + str(cur_user))
user_list = [cur_user]
# 如果當(dāng)前用戶是:3-學(xué)生,則查找對應(yīng)輔導(dǎo)員用戶
if cur_user.role == 3:
parent_id = cur_user.parent_id
# 根據(jù)用戶ID獲取用戶對象
parent_user = StudyUser.objects.get(user_id=parent_id)
print('根據(jù)用戶ID獲取用戶對象:' + str(parent_user))
user_list = [cur_user, parent_user]
# 獲取待完成任務(wù)列表,限制發(fā)布人
data_list = StudyScheduledTask.objects.filter(created_by__in=user_list).order_by('-pk')
# 查詢待完成任務(wù)列表
rsp_dict['data_list'] = data_list
context_object_name = "scheduled_task_list"
template_name = "study_system/home.html"
rsp_dict['html_file'] = 'study_system/task/dayTaskList.html'
rsp_dict['context_object_name'] = context_object_name
return render(request, template_name, rsp_dict)
def toNewDayTask(request):
'''
@方法名稱: 跳轉(zhuǎn)到新增定時務(wù)視圖
@作 者: PandaCode輝
@weixin公眾號: PandaCode輝
@創(chuàng)建時間: 2023-10-10
'''
rsp_dict = {}
rsp_dict["pageTitle"] = "新增定時任務(wù)"
# 'html_file': 'xxx.html' 動態(tài)指定模板頁面 ; 'menuTo': 'task' = 任務(wù)管理 ;
rsp_dict['html_file'] = 'study_system/task/addDayTask.html'
return render(request, "study_system/home.html", rsp_dict)
def addNewDayTask(request):
'''
@方法名稱: ajax請求, 表單視圖,新增定時任務(wù)
@作 者: PandaCode輝
@weixin公眾號: PandaCode輝
@創(chuàng)建時間: 2023-10-10
'''
# 初始化響應(yīng)容器
rsp_dict = {"result": "error", "errorMsg": "系統(tǒng)錯誤"}
# 是否ajax請求
if request.is_ajax():
try:
rest = request.POST
schedule_type = rest['scheduleType']
task_title = rest['taskTitle']
task_type = int(rest['taskType'])
task_description = rest['taskDescription']
reward_points = int(rest['rewardPoints'])
deadline_days = int(rest['deadlineDays'])
phone_num = rest['phoneNum']
scheduleDay = rest['scheduleDay']
scheduleTime = rest['scheduleTime']
# 定時時間 拼接日期時間
schedule_time = scheduleDay + " " + scheduleTime
# 獲取當(dāng)前用戶名
username = request.session.get('username')
# 根據(jù)用戶名獲取用戶對象
cur_user = StudyUser.objects.get(username=username)
print('根據(jù)用戶名查詢用戶對象:' + str(cur_user))
# 創(chuàng)建者ID,使用 StudyUser 對象賦值
created_by = cur_user
# 今天
# UTC格式當(dāng)前時區(qū)時間
t = time.localtime()
work_date = time.strftime("%Y-%m-%d %H:%M:%S", t)
print('當(dāng)前日期時間:' + str(work_date))
# 創(chuàng)建對象并保存到數(shù)據(jù)庫
studyScheduledTask = StudyScheduledTask(task_type=task_type, schedule_type=schedule_type,
schedule_time=schedule_time.strip(), task_title=task_title,
task_description=task_description, reward_points=reward_points,
deadline_days=deadline_days, phone_num=phone_num,
created_time=work_date, update_time=work_date,
created_by=created_by)
# 保存到數(shù)據(jù)庫是否成功
studyScheduledTask.save()
rsp_dict["result"] = "success"
except Exception as e:
rsp_dict["errorMsg"] = "新增定時任務(wù)保存到數(shù)據(jù)庫失敗."
# 成功與否都返回json數(shù)據(jù)格式
return JsonResponse(rsp_dict)
4,編輯頁面模板代碼:
4.1. 定時任務(wù)列表頁面
./mysite/study_system/templates/study_system/task/dayTaskList.html
# 更多代碼內(nèi)容請關(guān)注weixin公眾號: PandaCode輝
4.2.?添加定時任務(wù)頁面
./mysite/study_system/templates/study_system/task/addDayTask.html
# 更多代碼內(nèi)容請關(guān)注weixin公眾號: PandaCode輝
第二步:運行測試-任務(wù)管理功能
1,登錄用戶后,點擊查看定時任務(wù)列表頁面
?2,新增定時任務(wù)頁面
?循環(huán)周期:可以選擇每天1次,定時1次,每周幾1次,常用的還是每天1次周期。
文章來源:http://www.zghlxwxcb.cn/news/detail-715425.html
?-------------------------------------------------end -------------------------------------------------文章來源地址http://www.zghlxwxcb.cn/news/detail-715425.html
到了這里,關(guān)于Django實戰(zhàn)項目-學(xué)習(xí)任務(wù)系統(tǒng)-定時任務(wù)管理的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!