CRM开发学员课程系统——我的课程提交作业

 
#student/urls.py
#提交作业
url(r'my_homework/(d+)$',views.my_homework,name='my_homework'),

提交作业的文件是使用myDropzone,程序要新建一个文件放作业,文件目录为这样的格式:base_dir/class_id/course_record_id/studyrecord_id,使用chunks()把作业文件写入创建的文件:

#student/views.py
def my_homework(request,studyscore_id):
    """我的作业"""
    studyscore_obj = models.StudyRecord.objects.get(id=studyscore_id)
    if request.method == "POST":
        #print("request.FILES:",request.FILES)
        if request.is_ajax():
            homework_path = "{base_dir}/{class_id}/{course_recored_id}/{studycords_id}".
                format(base_dir=settings.HOMEWORK_DATA,class_id=studyscore_obj.student.enrolled_class_id,
                       course_recored_id=studyscore_obj.course_record_id,studycords_id=studyscore_obj.id)
            if not os.path.isdir(homework_path):
                os.makedirs(homework_path,exist_ok=True)
            for k,file_obj in request.FILES.items():
                with open("%s/%s"%(homework_path,file_obj.name),"wb") as f:
                    for chunk in file_obj.chunks():
                        f.write(chunk)
            return HttpResponse(json.dumps({"status":0,"msg":"file upload success"}))
    return render(request,"student/my_homework.html",{"studyscore_obj":studyscore_obj})

#通过学习记录表找到所关联的字段值
#student/homework.html {% extends 'index.html'%} {% block page-content %} {% load stu_tags %}
<div class="panel panel-default"> <div class="panel-body"> {{enroll_obj.enrolled_class.course}} </div> <div class="panel"> <!-- Default panel contents --> <!-- Table --> <h3>作业标题:{{studyscore_obj.course_record.homework_title}}</h3> <h3>作业详情:<pre>{{studyscore_obj.course_record.homework_content}}</pre></h3> <h3>老师评语:{{studyscore_obj.memo}}</h3> <h3>我的分数:{{studyscore_obj.score}}</h3> <h3>提交作业:</h3> <form id="filedropzone" method="post" action="{{request.path}}" class="dropzone dz-clickable">{% csrf_token %} <div class="dz-default dz-message"> <div class="dz-icon icon-wrap icon-circle icon-wrap-md"> <i class="fa fa-cloud-upload fa-3x"></i> </div> <div> <p class="dz-text">把证件信息拖放到这里</p> <p class="text-muted">最多可上传2张照片</p> </div> </div> </form> </div> </div> {% endblock%} {% block bottom-js %} <script> $(document).ready(function () { Dropzone.options.filedropzone ={ url:"{{request.path}}", paramName:"file",//The name that will be used to transfer the file maxFilesize:5,//MB addRemoveLinks:true, maxFiles:1, uploadMultiple:true, accept:function (file,done) { if(! file.name.endsWith(".zip")){ alert("只能上传zip文件"); //return false; }else{done();} }}; myDropzone.on("success",function (file,response) { /*Maybe display some more file information on your page*/ console.log('filex upload done....',response); }) }); function RegisterFormCheck() { if(myDropzone.files.length>1){ alert("必须只传一个文件!") return false; } if($("form :checkbox").prop("checked")){ $("form").find("[disabled]").removeAttr("disabled"); return true; }else{ alert("必须同意该协议"); return false; } } Dropzone.autoDiscover = false; myDropzone = new Dropzone("#filedropzone"); myDropzone.on("addedfile", function(file) { /* Maybe display some more file information on your page */ console.log("-------",file) }); console.log(myDropzone.files) </script> {% endblock %}
原文地址:https://www.cnblogs.com/venvive/p/11504013.html