1

I need to pass a dictionary from a python script to a Django app. Therefore I'm using the following:

def sendDataToApp(rssi_readings):
        url = 'http://localhost:8000/world/rssi_import'
        data = urllib.urlencode(rssi_readings)
        req = urllib2.Request(url, data)
        response = urllib2.urlopen(req)

The rssi_readings is the actual dictionary. Now in my Django app I have an url set up and do have a view defined as follows:

def rssi_import(request, rssi_readings):
    print "Test"
    cache.set('rssi_angle_reading', rssi_readings)
    return HttpResponse("Data submitted")

I'm not even seeing the print, so I think the server can't handle the data. After that I want to store the data in a cache, which shouldn't be a problem.

My dev server is up and running, but when I run the script I get an HTTP ERROR 500 with the following:

  File "autoorientation.py", line 245, in <module>
    main()
  File "autoorientation.py", line 241, in main
    sendDataToApp(rssi_readings)
  File "autoorientation.py", line 199, in sendDataToApp
    response = urllib2.urlopen(req)
  File "/usr/lib/python2.7/urllib2.py", line 126, in urlopen
    return _opener.open(url, data, timeout)
  File "/usr/lib/python2.7/urllib2.py", line 406, in open
    response = meth(req, response)
  File "/usr/lib/python2.7/urllib2.py", line 519, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/lib/python2.7/urllib2.py", line 444, in error
    return self._call_chain(*args)
  File "/usr/lib/python2.7/urllib2.py", line 378, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.7/urllib2.py", line 527, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 500: INTERNAL SERVER ERROR

I was hoping anyone can tell me what the problem is? Even trying to post a simple string didn't work. Thank you!!!

EDIT:

Here is my url.py. I have it split up in 2 files, but this should be the important one:

from django.conf.urls.defaults import patterns, include, url

urlpatterns = patterns('world.views',

    url(r'^$', 'index'),
    url(r'^setup/$', 'setup'),
    url(r'^redoscan/$', 'redoScan'),
    url(r'^rssi_import/$', 'rssi_import'),
)

EDIT2:

Ok, I have just tried to not send the data in the request from the script and now I actually get something. The dev server says:

[02/May/2012 15:32:46] "GET /world/rssi_import HTTP/1.1" 301 0
[02/May/2012 15:32:46] "GET /world/rssi_import/ HTTP/1.1" 200 14

So I'm very sure that the script works and my urls to, but somehow the stupid thing doesn't know what to do with the data.

8
  • 1
    Do you use CSRF, when POSTing data? Commented May 2, 2012 at 19:51
  • What does the Django app's log say? Commented May 2, 2012 at 19:55
  • And, just to be sure, the posting script isn't running in the same Django dev server, by any chance? Commented May 2, 2012 at 19:55
  • @AleksejVasinov No, I don't think I'm using CSRF because I don't habe a clue what it is! Commented May 2, 2012 at 19:57
  • 2
    @masterlampe rssi_readings won't be passed as a variable to the function, it'll be in request.POST. The error looks like you're net even getting to making the request which is why you're not seeing the print Commented May 2, 2012 at 20:03

2 Answers 2

3

I'm not sure why you have defined your view function like this:

def rssi_import(request, rssi_readings):

I suppose that MIGHT work with some tricky magic in your urls.py definitions.

But I think the much more direct approach is: just read the POST data out of the request:

def rssi_import(request):
    rssi_readings = request.POST
    # do something with rssi_readings here
    return HttpResponse("Data submitted")    

Perhaps you should check if this even is a POST, and if your later functions peform side effects on the returned dictionary, you might at least want a shallow copy:

def rssi_import(request):
    if request.method == 'POST':
        rssi_readings = dict(request.POST)
        # do something with rssi_readings here
        return HttpResponse("Data submitted")
    if request.method == 'GET':
        return HttpResponse("rssi_import() reached.  Now POST some RSSI data to me...")

Is that close to what you need?

EDIT/UPDATE:

Don't forget, the view function still has to return a valid response! I've added that above, using your "return" line. Also: a GET handler so you can debug server issues (maybe in your urls.py) with a simple brower call.

Sign up to request clarification or add additional context in comments.

7 Comments

Well, I just tried putting that in, but without any luck. It's still the same error.
Are you sure rssi_import() is even being called? Could be an error in your urls.py, too.
I of course still had the return in there. I'm very certain it's being called because my django dev server displays [02/May/2012 15:11:16] "POST /world/rssi_import HTTP/1.1" 500 63689. And I think I have the urls.py correct.
a) That line to me says "I received a POST for <this URL>". It doesn't mean that your view function was correctly called, or called at all.
Note your urls.py defines the URL WITH a trailing slash, but your client does NOT. (This actually matters.) Your server is performing a "redirect" (the "301" line)... but I'm not positive if the client re-POSTs the original body after a 301. Have you tried: a) in the client, print out the urlencoded DATA (to make sure there are no anomalies, b) in the view code, printing out the contents of request.POST?
|
0

Ok, I finally figured it out.

@Dan H You were right with the trailing slash, it actually made it big difference. Once removed I actually got a 403 error.

After checking out with I got an 403 error I found something about CSRF, just as @Aleksej Vasinov already mentioned in one of his comments. I actually didn't even know I was using that, but it seems to be almost the standard way in the settings file. I now have an CSRF exempt for this specific view and everything works fine.

I can now post data and am very happy. Thank you for all your great ideas. I'll be back on stackoverflow. :)

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.