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.