Django - How to get the length of the array through GET request? - django

I have a http get request that has an array, particularly:
[29/Sep/2014 16:04:47] "GET /webapp/ajax_retreiveServerSideUsernames/?draw=1&columns%5B0%5D%5Bdata%5D=0&columns%5B0%5D%5
Bname%5D=&columns%5B0%5D%5Bsearchable%5D=true&columns%5B0%5D%5Borderable%5D=true&columns%5B0%5D%5Bsearch%5D%5Bvalue%5D=&
columns%5B0%5D%5Bsearch%5D%5Bregex%5D=false&order%5B0%5D%5Bcolumn%5D=0&order%5B0%5D%5Bdir%5D=asc&start=0&length=10&searc
h%5Bvalue%5D=&search%5Bregex%5D=false&csrfmiddlewaretoken=Something&_=1412028280135 HTTP/1.1" 200
1436
My question is that how do I get the "length" of a particular array in a GET request?
For example the array order:order[i][column].
However, there may be cases with unkown array size.
If I know that i = 0, then I can use request.get['order[0][column]'] to get the content of the first array's column data.
I tried to use request.GET.getlist('order[column]') or request.GET.getlist('order') with no avail.

Related

How can i pass empty array for specific field in postman (form-data)?

I'm using laravel framework and need empty array to do some actions, and my request is Post with form-data. i dont't want it JsonRequest i want empty array with form-data request. please help.
I'm trying with above, but its returning null, i want empty array.
What i want in backend is [ ] empty array.
hi you can use null value instead of an empty array
when you want send a array for example you send key[index] : value
for a null value you should send just key without bracket and value with empty string for example key : "" or key : in postman

Did not receive # and % symbols via Django GET URL

I was sending the get request like this:
http:121.0.0.1:8000/userlogin/userName=test&password=12345#%
but accepted like this:
"GET /userlogin?username=admin&password=12345 HTTP/1.1" 403 2868
The # and % symbols were not received.
I cannot understand what happened here?
URL parameters have certain allowed characters in them, as explained in this question: https://stackoverflow.com/a/1455639/1726625
I suggest editing your password settings, and allowing/disallowing those characters and/or changing the method that you're using to not passing them as a URL parameter.
Could you ellaborate on the case that you need that for?

QueryDict getlist returns empty list

I'm sending from frontend object with one property which equal to array.
In backend I need to get data from that array.
when i write request.POST i see:
<QueryDict: {u'response[0][doc_id]': [u'14'], u'response[1][uuid]': [u'157fa2ae-802f-f851-94ba-353f746c9e0a'], u'response[1][doc_id]': [u'23'], u'response[1][data][read][user_ids][]': [u'9'], u'response[0][uuid]': [u'8a0b8806-4d51-2344-d236-bc50fb923f27'], u'response[0][data][read][user_ids][]': [u'9']}>
But when i write request.POST.getlist('response') or request.POST.getlist('response[]') i get
[]
request.POST.get('response') doesn't work as well (returns None).
What is wrong?
Because you don't have either response[] or response as keys, you have the literal strings response[0][doc_id] and response[1][uuid] etc.
If you want to use a structure like this, you should send JSON rather than form-encoded data and access json.loads(request.body).

Django: View-function fails on saving model

I'm currently playing around with some selfmade online Text-RPG and now have to deal with some quite strange problem which I hope someone here can solve.
I'd like to toggle a boolean field called 'rp_willing' in the model 'Character' when clicking on certain links. For that I have a view function called 'character_toggle_rp_willing'.
The field definition:
class Character(models.Model):
...
rp_willing = models.BooleanField(default=False)
...
The view function:
#decorators.login_required
def character_toggle_rp_willing(request, id):
character = get_object_or_404(Character, pk=id)
character.rp_willing = not character.rp_willing
character.save()
return redirect(request.META.get('HTTP_REFERER','/accounts/overview'))
It queries the character in question from the database by it's id. Then 'rp_willing' is toggled and the character is saved before the function redirects back to the previous page.
The problem now seems to lie in the call of 'character.save()'. I've placed some print-functions for debugging purposes and everything else seems to work as intended. Also, the whole functions works absolutly correct if called from a template of the same app. But if the template is from a differente app, the save-function just isn't executed. There is no exception which could give any hints. I don't really know, how to debug in this case. I couldn't find the code of the built-in save()-function either.
Is the save()-function somehow related to the passed request? Or is there something I have just overlooked?
EDIT:
What the Dev-Server says...
Works as intended:
[28/Aug/2011 17:06:53] "GET /accounts/overview HTTP/1.1" 200 2283
[28/Aug/2011 17:06:59] "GET /accounts/character/toggle_rp_willing/1/ HTTP/1.1" 302 0
[28/Aug/2011 17:06:59] "GET /accounts/overview HTTP/1.1" 200 2285
Fails on saving:
[28/Aug/2011 17:03:26] "GET /village/ HTTP/1.1" 200 1531
[28/Aug/2011 17:03:28] "GET /accounts/character/toggle_rp_willing/1/ HTTP/1.1" 302 0
[28/Aug/2011 17:03:28] "GET /village/ HTTP/1.1" 200 1531
Okay, turned out I was just looking from a wrong angle. The problem is not my 'character_toggle_rp_willing' for it saves the character as it should. But the view it redirects to does not retrieve the character-data from the database. Instead it uses the data stored in a session variable - the old data. And then there is a method called, that saves that old data, so nothing changes in the end... I guess I should look for a good debugger.

Django - Internationalisation and Google Maps api v3

I have a Django app and have integrated some Google maps via the v3 api. After a week of discovery and playing around everything was working fine, until...
I changed the language on my app by clicking on a flag on a form that POSTS the action to /i18n/setlang/, which is what Django uses to change the language. Now the new language is showing up, but the maps aren't. In the Chrome debugger it's giving the following error:
Failed to load resource: the server responded with a status of 400 (Bad Request)
StaticMapService.GetMapImage
The following is the Chrome debugger header content for the error:
1.
Request URL:
http://maps.googleapis.com/maps/api/js/StaticMapService.GetMapImage?1m2&1iNaN&2iNaN&2e2&3u8&4m2&1uNaN&2uNaN&5m3&1e3&2b1&5sen-US&token=128748
2.
Request Method:
GET
3.
Status Code:
[400 Bad Request]
400 Bad Request
4. Request Headers
1.
Referer:
http://127.0.0.1:8000/uns/uns_cities_form/Mu%C4%9Fla/
2.
User-Agent:
Mozilla/5.0 (X11; U; Linux x86_64; en-US) AppleWebKit/534.3 (KHTML, like Gecko) Chrome/6.0.472.63 Safari/534.3
5. Query String Parameters
1.
1m2:
2.
1iNaN:
3.
2iNaN:
4.
2e2:
5.
3u8:
6.
4m2:
7.
1uNaN:
8.
2uNaN:
9.
5m3:
10.
1e3:
11.
2b1:
12.
5sen-US:
13.
token:
128748
6. Response Headers
1.
Content-Length:
1350
2.
Content-Type:
text/html; charset=UTF-8
3.
Date:
Sun, 06 Feb 2011 20:29:59 GMT
4.
Server:
staticmap
5.
X-XSS-Protection:
1; mode=block
If I set the language back to English all works fine again...
Ok, so there is nothing to do with any translation whilst loading the map, but I'm figuring that Django has changed something or other which is disrupting the Http request, although to be honest I have no idea what is going on. The following is the options and the call to the map
//Map Options
myOptions =
{
zoom: 8,
center: latlng,
mapTypeId: google.maps.MapTypeId.HYBRID,
streetViewControl: false,
mapTypeControl: true,
mapTypeControlOptions: {
style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
},
}
//Create the map
map = new google.maps.Map(elem,myOptions);
Has anyone come across this, or can anyone throw some light on what might be happening?
I suspect Django's Format Localization could cause that.
When using Django's formatting system,
dates and numbers on templates will be
displayed using the format specified
for the current locale. Two users
accessing the same content, but in
different language, will see date and
number fields formatted in different
ways, depending on the format for
their current locale.
So depending on the language, it could use a dot or comma as separater. This could mean, that instead of 2 parameters, you actually got 4 parameters, because their decimal separator is a comma.
You could try to apply floatformat to prevent this from happening.
{{ city.0.0.latitude|floatformat:6 }},{{ city.0.0.longitude|floatformat:6 }}
Ok, got the core problem.
The Django latitude and longitude variables in my database are Decimal Fields. When I pass them from the template to the javascript function they are passing the first part of the decimal to the first argument and the second part of the decimal to the second argument instead of being two separate arguments.
for example... 36.620556 is a latitude in the following
onclick='showmap({{city.0.0.latitude}},{{city.0.0.longitude}});'
The function for showmap header is
showmap(lat,lng)
{...
However, lat and lan come out as follows
lat = 36 and lan = 620556
However the strange thing is that this doesn't happen if the default language is in English...
I'll play some more and see what I can find out...
SOLUTION:
As Reiner suggested it was the localisation that was the problem. I was getting commas in my decimal points so I wrote a quick template tag to replace the commas with a dot.
def fixlatlan(Object):
o = str(Object)
o = o.replace(",", ".")
return o
register.filter('fixlatlan', fixlatlan)