This is my Javascript code:
expr = "A simple string"
$.ajax({
type: "GET",
url: "/ajaxrequest/get_dt_paragraphs?expr=" + expr,
timeout: 8000,
success: function(data) { doSomething(); }
});
In my django view, I wrote this method:
def get_dt_paragraphs(request):
if request.method == 'GET':
my_string = request.GET["expr"]
# print on the console for debugging purposes.
print my_string
# Do other stuff
The output on the console is [u”A simple string”], so I discovered that the parameter name returns an array. Of course, I could write request.GET[“expr”][0] to get the string, but I would like to know why I get a list instead of the string, and how can I avoid this to write better and more elegant code.
GET and POST objects are of class QueryDict, which is designed to deal with multiple values per key (docs).
If you are sure that you want exactly one value I would suggest
This falls back to empty array in case ‘expr’ is not available, but this should be idiomatic Python anyway, as [] has Boolean value of False.