Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8167551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:13:45+00:00 2026-06-06T20:13:45+00:00

I am trying out ajax file upload in django .I coded the javascript ,django

  • 0

I am trying out ajax file upload in django.I coded the javascript ,django view as below.The file gets uploaded successfully.Then I wrote a test method for the view.When the test is executed ,the file gets successfully uploaded to the destination folder.But ,I get some strange error.

This particular error does not occur when ajax upload is performed in the application.It occurs only when the test is executed.

$(document).ready(function(){
        $(document).on('change', '#fileselect', function(e){
            e.preventDefault();
            uploadFile(e);
        });

function uploadFile(e){
    var form = $('#fileform').get(0);
    var formData = new FormData(form);
    var file = $('#fileselect').get(0).files[0];
    var xhr = new XMLHttpRequest();
    formData.append('file', file);
    xhr.open('POST', 'upload/', true);
    xhr.send(formData);

    xhr.onreadystatechange=function(){
       if (xhr.readyState==4 && xhr.status==200){
          var data = $.parseJSON(xhr.responseText);
          var uploadResult = data['message']

          if (uploadResult=='failure'){
             displayErrorMessage('failed to upload');
          }else if (uploadResult=='success'){
          }
       }
    }
}
...

The django view

def ajax_upload(request):
    retvals = {}
    message="failure"
    if  (request.method == 'POST'):
        if request.FILES.has_key('file'):
            file = request.FILES['file']
            print 'file=',file
            with open(settings.uploadfolder+'/'+fname, 'wb+') as dest:
                for chunk in file.chunks():
                    dest.write(chunk)
            message="success"
    retvals['message']= message
    serialized = json.dumps(retvals)
    print 'serialized=',serialized
    if message == "success":
        print 'success'
        return HttpResponse(serialized, mimetype="application/json")
    else:
        return HttpResponseServerError(serialized, mimetype="application/json")

snippet from urls.py file

urls.py

...
url(r'^upload/$', 'myapp.views.ajax_upload',name='ajax_upload'),
...

Finally,here is the test method

class UploadTest(TestCase):
    def setUp(self):
        super(UploadTest,self).setUp()
        self.client.login(username='me',password='mypass')

    def test_upload(self):
        fname = os.path.join(settings.testfolder,'mydoc.doc')
        with open(fname) as fp:
            resp = self.client.post(self.client.post(reverse('ajax_upload'),{'file':fp}, HTTP_X_REQUESTED_WITH='XMLHttpRequest'))

As you can see,I’ve put some print statements in the view code ,and they show that the request.FILES['file'] has correct value (ie mydoc.doc) and the upload was successful(I can see the file copied to the dest folder).

Now here is the console output which shows the error messages

file= mydoc.doc
serialized= {"message": "success"}
 success
E
======================================================================
ERROR: test_upload (myapp.tests.UploadTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/me/dev/python/django/myapp/tests.py", line 143, in test_upload_file
    resp = self.client.post(self.client.post(reverse('ajax_upload'),{'file':fp}, HTTP_X_REQUESTED_WITH='XMLHttpRequest'))
  File "/home/me/Django-1.4/django/test/client.py", line 449, in post
    response = super(Client, self).post(path, data=data, content_type=content_type, **extra)
  File "/home/me/Django-1.4/django/test/client.py", line 252, in post
    parsed = urlparse(path)
  File "/usr/lib/python2.6/urlparse.py", line 108, in urlparse
    tuple = urlsplit(url, scheme, allow_fragments)
  File "/usr/lib/python2.6/urlparse.py", line 147, in urlsplit
    i = url.find(':')
AttributeError: 'HttpResponse' object has no attribute 'find'

----------------------------------------------------------------------
Ran 1 test in 0.200s

FAILED (errors=1)

I could not make out why this occurs..can someone help me figure this out?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T20:13:47+00:00Added an answer on June 6, 2026 at 8:13 pm
    resp = self.client.post(self.client.post(...))
    

    Don’t worry, happens to everybody 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to use Valum's Ajax Upload to do file uploads on a
I have been trying to use an ajax-style file upload component (like the dojox
I have downloaded ajax control toolkit (newest) and trying out fileupload. It's really good
I'm trying to figure out how to load a new image with ajax when
Spent a solid hour trying to sort out why on earth this (coffeescript) $.ajax
Ran into an Out of Stack Space error trying to serialize an ASP.Net AJAX
I'm trying to port a portion of AJAX code I wrote over to the
I'm trying to parse the total funding dollar amount out of this JSON file:
I'm trying to load a url of an xml file into my page then
I've set up a drag and drop file upload script in JS (AJAX POST)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.