Monday 18 February 2013

Multilingual support in Django

  1. Go to settings.py file and
    USE_I18N = True
    USE_L10N = True
    Go to middleware_classes section
    add "'django.middleware.locale.LocaleMiddleware"
  2. Create Template and write code
    {%load i18n %}
    {% trans "Hi I am Prashant Gaur" %}  (you need to put all data in trans or any django template block)
  3. Create language files:
    go to project directory-
    mkdir locale
    django-admin.py makemessages -l en      (english
    django-admin.py makemessages -l sv       (swedish)   
    If you are getting any error so it means you are missing xgettext (ubuntu)
    sudo apt-get install gettext
  4. python manage.py compilemessages
  5. Now change language in browser to swedish it will automatically convert into swedish .
  6. You can create many language files and multilingual environment is ready
     

add custom link href in django admin interface

Hi :)
step1 ) first go to your app folder and them select admin.py file
step 2 ) now
class Player(admin.ModelAdmin):     
    list_display =['id','Name','Mail','Facebook_link']
    def facebook_link(self,obj):
        return '<a href="%s">Facebook</a>'%('http://www.facebook.com')
    facebook_link.allow_tags = True
    facebook_link.short_dscription = "Facebook"


Wednesday 13 February 2013

find result after passing list in filter query in django

Like You have a Table Student with column "name"
so if you need to filter your object based in given list with some names .


Answer :

namelist =['first','second','third','fourth']
obj = Student.objects.filter(name__in=namelist)