2

I'm unable to upload a file using django. When I hit submit button I get "This webpage is not available. The webpage at http://127.0.0.1:8000/results might be temporarily down or it may have moved permanently to a new web address." error in chrome.

For the file upload HTTP query the corresponding webserver's log entry is:

[02/Jul/2010 17:36:06] "POST /results HTTP/1.1" 403 2313

This is the form:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 

<head> 
 <title>Content Based Image Retrieval System</title>
 <link rel="stylesheet" href="site-content/css/style.css" />
</head>

<body>
 <div><img src="site-content/images/logo.jpg" /> </div>
 <form name="myform" action="results" method="POST" ENCTYPE="multipart/form-data>
  <div align="center">
  <br><br>
  <input type="file" size="25" name="queryImage">
  <br><input type="submit" value="Search"><br>
  </div>
 </form>
</body>

entry in urls.py:

(r'^results$',upload_and_search),

view that handles the file upload:

def upload_and_search(request):
    if request.method != 'POST' or request.FILES is None:
        output = 'Some thing wrong with file uploading'
    handle_uploaded_file(request.FILES['queryImage'])
    output = 'success'
    return HttpResponse(output)

def handle_uploaded_file(f):
    destination = open('queryImage', 'wb+')
    for chunk in f.chunks():
        destination.write(chunk)
    destination.close()

EDIT:

I've also tried changing the destination line destination = open('queryImage', 'wb+') to destination = open(os.environ['TMP']+'\\'+filename, 'wb+'). Its still giving the same error. Also the file I'm trying to upload is less than 2.5MB.

EDIT 2:

I've added a print statement in the first line of upload_and_search.Its not printing anything. i.e.. its not not even entering the function. I also checked if something is wrong with my url mapping by directly accessing the url http:// 127.0.0.1:8000/results. Its working fine. I think there is some problem with server configuration. I've no clue how to configure this server or what to configure. I'm stuck! I've no clue about what to do.

5
  • What are the permissions for the directory that you're trying to write queryImage in? Commented Jul 2, 2010 at 12:23
  • 1
    you have also error in ENCTYPE="multipart/form-data <-missing " Commented Jul 2, 2010 at 13:23
  • +1 for supplying all the relevant info in your very first question. Looks like somebody read the FAQs. ;-) Commented Jul 2, 2010 at 15:05
  • Can you put in some logging (or even just print statements to the console) to see if Django is hitting the upload_and_search view at all, and if it's correctly triggering the handle_uploaded_file function? Commented Jul 2, 2010 at 15:30
  • @Daniel Roseman : please see the editied question. Commented Jul 2, 2010 at 15:52

2 Answers 2

2

I guess its because of csrf http://docs.djangoproject.com/en/dev/ref/contrib/csrf/

try changing your from

<form name="myform" action="results" method="POST" ENCTYPE="multipart/form-data">{% csrf_token %}

an the view generating it

from django.core.context_processors import csrf
from django.shortcuts import render_to_response

def showIndexPage(request):
    c = {}
    c.update(csrf(request))
    return render_to_response("index.html", c)
Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

filename = f['filename']
destination = open('%s/%s' % (MEDIA_ROOT, filename), 'wb')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.