Tag Archives: django

Django multilingual: select translation instead of ‘None’

If you use the django-multilingual module for multilingual content, you need to add a translation for every language your entity could be seen through. Otherwise you get 'None' as a title (which is way too blurry and quite indescribable). Thanks to my colleague and friend lpetrov I found a way to pass through this limitation and print any possible translation if the current one is not available.

I've added a translation.py file in my project which I import in all my apps when necessary. Here there is the code:

  1. def get_local_str_or_other(obj, field_name):
  2. result = getattr(obj, field_name)
  3. if result:
  4. return result
  5. else:
  6. field_for_search = field_name + "_"
  7. translations = [getattr(obj, field_for_search + lang[0].replace("-","_")) for lang in settings.LANGUAGES]
  8. for translation in translations:
  9. if translation != None:
  10. return unicode(translation)
  11.  

Later I import the function in my models and I redefine the __unicode__ function. If the name I want to print by default is the field 'title', we have the following:

  1. def __unicode__(self):
  2. return get_local_str_or_other(self, "title")
  3.  

 

del.icio.us Digg DZone Facebook Google Google Reader Magnolia reddit SlashDot Technorati ReadMe.ru Dobavi.com Dao.bg Lubimi.com Ping.bg Pipe.bg Svejo.net Web-bg.com

Django debugging (presentation)

 I found a great presentation on Django debugging from Simon Willison.

It's a presentation on "Django 101 in troubleshooting". You could find information how to address any errors from the error pages that django throws away, how to follow stack traces and how to debug information in variety of ways.

Simon pays attention to the basic and the core of all debugging tools - the console. Every single output pass through the console itself so it is the mother of all outputs. Other popular options is the python debugger under the pdb package. 

I personally do like django-debug-toolbar for my apps. It is quite useful for taking care of the HTTP requests and response, passed variables across the website and so on. You could check the number of SQL queries and the load time of your website. Another important feature is printing the list of all templates used for rendering a web page. You could check the settings list for the current page load as well as the signals being called at the moment.

More on the django-debug-toolbar - Rob speaks here.

del.icio.us Digg DZone Facebook Google Google Reader Magnolia reddit SlashDot Technorati ReadMe.ru Dobavi.com Dao.bg Lubimi.com Ping.bg Pipe.bg Svejo.net Web-bg.com

Python, SVN and Android

 

Three things with not much in common, except that these are the three interesting things around me these days.

For the last 2 weeks I do python development (actually Django) for an international project while I'm ending my CakePHP and Digital Signer applications. I set up Subclipse in my Eclipse because I got tired of committing twice some of my files. This way my SVN is connected to the project in the IDE and the new data is deployed in an easy and beautiful way without any console commits.

The Django projects are fine as well. I do researches over the multilingual applications. So far I found url-locale and multilingual modules which serve to detect the language via the URL and create a multilingual content while create the intermediate tables for that. I continue researching the session system in order to save the languages there where switching around.

As for the Android, I plan to do my first app for almost a year. Recently I had a few job proposals for Android development plus the Mtel contest for Android app. That's why I downloaded the SDK and the Android plugin and started from the Hello World tutorial online. More researches are done and I am willing to test a more complex application with Internet activity, of course.

del.icio.us Digg DZone Facebook Google Google Reader Magnolia reddit SlashDot Technorati ReadMe.ru Dobavi.com Dao.bg Lubimi.com Ping.bg Pipe.bg Svejo.net Web-bg.com