Saturday, 20 September 2014

Accessing dictionary values from a list of dictionaries in django... Wait . What?

This post deals with a question that has already been answered on stackoverflow here . I had to google  a lot, trying to phrase the exact words that would lead to this result. So, I am explaining the answer here as well( apart from the link provided) for myself and the others who find this blog before facing that problem (or it's solution).


I was in a situation in which i had to display the details of some users on the admin page of my django app. The list of users can be obtained in 1 line :

                     users.objects.filter(#myfiltercriteria)

The queryset returned can be transformed to a list of dictionaries with :

                    users.objects.filter(#myfiltercriteria).values()


Now the problem was how to retrieve these values. The simpler way is to retrieving the required values in the view itself and compile it in a list and then use the built in template tag of django for list to access the elements. 

Okay, I maybe wrong depending on the use case. (I was wrong in my case, as I had to retrieve the SAME set of values for all the users.) 

The better and efficient way is to make custom template tags. For the sake of better explanation, let us access the name attribute of the users dictionary passed to a template. 

   So, the custom filter tag is (credits :culebron )
                 
                    @register.filter
                    def get_item(dictionary, key):
                    return dictionary.get(key)

So, inside your template, you would access the names like this :

                   for user in users:
                       {{ user|get_item:item.name }}

This is extremely useful and neat but is not offered by django by default.

For a reference to see how custom template tags are made see this


Thursday, 4 September 2014

Always check the error log

5 hours trying to debug a very simple problem.. I've learned my lesson.

Always check the error log

I cannot emphasize this point more, it can save you lots of time. That's why it is there!

So, I made a web app in Codeigniter framework. All the development took place on my windows computer. Everything went perfect, web app fully functional in on localhost. Then i decided to launch it, on a Linux server (Ubuntu to be precise). I configured the database settings and started the server. The web app uses a login on its first page itself . It displayed the form correctly. I entered the login id and password only to be met b a blank page .I have been greeted many a times by 404 pages, but this was a new guest. A frequent user of web developer console, I quickly found out that it was a 500 Internal Server Problem. After a google search i found that it happens because of some programming  issue on the page/site. This was the first time I thought google was wrog, and this thougt was not baseless.. the same app was running perfectly on my PC. So , i searched here and there and everywhere (:P) . Tried everything , no perfect solution.

Then i posted this question on Stackoverflow here . What i need was just 1 comment (thanks @Prix) .
My error log just threw the syntax error on my face. I was bewildered . Why?
Again google. I found that the code i had written took help of a feature that had been introduced in PHP 5.4 while my server was running PHP 5.2 . The feature is called  function array dereferencing. It means accessing the array values at a particular index, the array being returned by a function. It meant that i had to use a temporary variable to store the returned value of the array ad then access array index from it .

Obviously the other solution was to use PHP 5.4 But to my dismay, the truth was that PHP 5.4 repos are not available for ubuntu 12.04. So i followed the instructions  here . However it did not work and i am still using php 5.2, but without issues.