有一个系统警报(13,'权限被拒绝'),如何解决?

时间:2010-04-25 06:06:31

标签: python django file-upload

def upload_file(request, step_id): 
    def handle_uploaded_file (file):
        current_step = Step.objects.get(pk=step_id)
        current_project = Project.objects.get(pk=current_step.project.pk)

        path = "%s/upload/file/%s/%s" % (settings.MEDIA_ROOT, current_project.project_no, current_step.name)
        if not os.path.exists (path): 
            os.makedirs(path)
        fd = open(path)
        for chunk in file.chunks():
            fd.write(chunk)
        fd.close()

    if request.method == 'POST':
        form = UploadFileForm(request.POST, request.FILES)
        if form.is_valid():
            handle_uploaded_file(request.FILES['file'])
            return HttpResponseRedirect('/success/url/')
    else:
        form = UploadFileForm()
    return render_to_response('projects/upload_file.html', {
                                                      'step_id': step_id,
                                                      'form': form,
                                                      })

1 个答案:

答案 0 :(得分:2)

确保path具有必要的权限。运行python / django进程的用户需要具有write权限。 chmod 0777的路径 - 这不是一个很好的生产模式,但它会快速验证文件系统权限是否是问题的根源。