tags:

views:

56

answers:

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"&gt; 
<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.

A: 

Try this:

filename = f['filename']
destination = open('%s/%s' % (MEDIA_ROOT, filename), 'wb')
histrio
No, this didn't work. Its showing the same error.
pecker
A: 

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)
claws