Question

I am learning Django so I've set up a very simple form/view/url example

  • Django Version 1.5.1
  • MATLAB Version R2012A

forms.py

from django import forms
import json

class json_input(forms.Form):

    jsonfield = forms.CharField(max_length=1024)

    def clean_jsonfield(self):
        jdata = self.cleaned_data['jsonfield']
        try:
            json_data = json.loads(jdata)
        except:
            raise forms.ValidationError("Invalid data in jsonfield")
        return jdata

views.py

from django.http import HttpResponse
from rds.forms import json_input

def testpost(request):

    if request.method == 'GET':

        form = json_input(request.GET)
        if form.is_valid():
            return HttpResponse('Were Good Get',mimetype='text/plain')

    elif request.method == 'POST':

        form = json_input(request.POST)
        if form.is_valid():
            return HttpResponse('Were Good Post',mimetype='text/plain')

    else:
        return HttpResponse('Not GET or POST.',mimetype='text/plain')

This view is mapped to the url in urls.py

url(r'^test2$','rds.views.testpost'),

So when I jump into the python manage.py shell on the local machine django is on I can issue the following commands and get the expected responses:

>>> from django.test.client import Client
>>> c = Client()
>>> r = c.post('/test2',{'jsonfield': '{"value":100}'})
>>> print r
Content-Type: text/plain

Were Good Post
>>> r = c.get('/test2',{'jsonfield': '{"value":100}'})
>>> print r
Content-Type: text/plain

Were Good Get

However when I jump into MATLAB on an external machine and issue the following commands. (Note doing this from MATLAB is a project requirement)

json = '{"value":100}';

% GET METHOD FOR JSON FORM
[gresponse,gstatus]=urlread('http://aq-318ni07.home.ku.edu/django/test2','Get',{'jsonfield' json});

% POST METHOD FOR JSON FORM
[presponse,pstatus]=urlread('http://aq-318ni07.home.ku.edu/django/test2','Post',{'jsonfield' json});

>> gresponse
    gresponse =
    Were Good Get
>> presponse
    presponse =
         ''

I have searched around for a solution and really cant find anything. I've hit on it potentially being an issue with the CSRF (which I am still figuring out). Any hints or thoughts would be much appreciated.

Thank you.

EDIT:

Django is exposed through Apache, here is the configuration.

################################################
# Django WSGI Config
################################################

WSGIScriptAlias /django /var/django/cdp/cdp/wsgi.py
WSGIPythonPath /var/django/cdp

<Directory /var/django/cdp/cdp>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>

################################################
Was it helpful?

Solution

how are you exposing your django app for MATLAB? The very first thing is to check your access logs, is your server even getting a request? If so anything in its error logs?

Are you using the built in developkment server? python manage.py runserver 0.0.0.0:8000 If so make sure that you can accept requests on that port

If you are serving it through another server i believe you have to whitelist the IP that you are making request from MATLAB, by adding it to ALLOWED_HOSTS

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top