Upload files in Django using Uploadify getting HTTP response code 403

1.6k Views Asked by At

I am trying to upload files in Django using Uploadify following this tutorial. I have carried out all the steps properly. In addition to that I have made a model where I store information regarding the files that are being uploaded.

My template contains this

<script type="text/javascript" src="{{ STATIC_URL }}uploadify/swfobject.js"></script>
<script type="text/javascript" src="{{ STATIC_URL }}uploadify/jquery.uploadify.v2.1.4.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
        $('#file_upload').uploadify({
            'uploader'  : '{{ STATIC_URL }}uploadify/uploadify.swf',
            'script'    : '{% url upload_file %}',
            'cancelImg' : '{{ STATIC_URL }}uploadify/cancel.png',
            'auto'      : true,
            'multi'     : true,
            'fileExt'   : '*.doc;*.docx;*.pdf',
            'fileDesc'  : 'Upload Docs',
            'queueID'   : 'file_upload_queue',
            'removeCompleted': false,
        });
    });
</script>

<h2 id="smallHeading"> Upload your Documents </h2>
<input id="file_upload" type="file" name="Filedata" />
<a href="javascript:$('#file_upload').uploadifyUpload();">Upload Files</a>

My url.py file contains this line

  url(r'^upload/$', 'userprofile.views.upload_file', name='upload_file'), 

and the view is

@csrf_exempt
def upload_file(request):
    if request.method == "POST":
        upload_file = request.FILES['Filedata']
        upload_instance = Uploads.objects.create(creator = request.user, uploaded_on = datetime.datetime.now())
    upload_instance.file.save(upload_file.name,upload_file)
    upload_instance.save()

    response = HttpResponse()
    response.write("%s\r\n" % upload.name)
    return response

and my models.py contains

def get_file_path(instance, filename):
    return os.path.join('userprofiles', str(instance.creator.id), filename)

class Uploads(models.Model):
    file = models.FileField(upload_to=get_file_path)
    creator = models.ForeignKey(User)
    uploaded_on = models.DateTimeField()

But nothing is happening here. All I get is the upload button, I select the files but nothing happens. Can anybody help me in finding out what is happening? It seems the script is not being fired. I know this because previously I had an error in my views but no error came up.

Any help is deeply appreciated.

Edit:

I am not seeing anything in the firebug console, and the net panel also does not show any POST request but the django debugger shows the POST request for file upload with a 403 response. I have used csrf_exempt then why should it be forbidden?

Solution: I have finally solved the problem. About the 403 error, I found out something interesting from this link. If a view has the decorator csrf_exempt and if there is 404 error for that view then django shows it 403 error.

The actual problem was that initially my link was wrong, so when I corrected it I stopped getting the 403 error, but I started getting a 500 error. This meant something was wrong in the view. And since uploadufy does not log anything in the firebug, I had to debug by stepping through line in my view.

It seems that the request object sent by the uploadify script is not complete and the line creator = request.user while creating the object was giving me the error. Other than this line, the code posted above works completely.

1

There are 1 best solutions below

0
On

I suggest debugging this further to see whether the issue lies in the client-side JavaScripts or the server-side Django code.

Download the Firebug plugin for Firefox. Load your page. Open Firebug and go to the "Net" tab. Make sure that all the JavaScripts are loaded. Next, check the console to see if any JavaScript bugs are present. The next check the Django logs to see if any HTTP requests are coming through and perhaps have errors in them.

This should help you get more information, which will help you resolve your issue. You can always update your question with additional information you gather from the above steps.