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)



Friday, 9 November 2012

Apache/2.2.15 (CentOS) Server forbidden error . not permission to access FOLDER


Forbidden

You don't have permission to access / on this server.

Apache/2.2.15 (CentOS) Server at 1.208.52.12 Port 80
CENT OS 

for changing file permission in CENT OS 

go to ur project directory ... like  /opt/prashant/project_directory/

write command :

chmod -R 755 /app_name 




755  in linux number notation for file permission


Prashant Gaur

Wednesday, 17 October 2012

convert queue into stack and make list work as queue and stack both

now we will see how one can convert QUEUE into STACK into python -- !


from collections import deque

list =[1,2,3,4,5]

queue_list = deque(list)

now queue_list will work as a queue object .... :)
---------------------------------------------------------------
you can extract any element from queue_list by using--

queue_list.popleft()
----------------------------------------this all thing is working as a queue object
now we just need to use a simple method pop() to work queue_list as a stack
-------------------------------------------------------------------------------------------------
queue_list.pop()
u can use also queue_list.popleft()  to work this list as a queue.


Thank You
Meet Me
Prashant Gaur


Monday, 1 October 2012

custom enum field in django to support mysql ENUM datatype :) :)

Hi friends ,

  As we know enum data type is used to reduce memory in database . we can pass string as input and can get output as string ---but in really it will strore in DB as 1 digit number starts from 0  with increase of 1 .
   but django does not provide us default django field so we need to make our own CUSTOM ENUM FIELD . you can go through djnago docs to know about it Django DOCS 

but for enum you just make youe own class



# Class which describe Custom Field of name Enum

class EnumField(models.Field):

    def __init__(self, *args, **kwargs):
        kwargs['max_length'] = 104
        super(EnumField, self).__init__(*args, **kwargs)
        if not self.choices:
            raise AttributeError('EnumField requires `choices` attribute.')

    def db_type(self):
        return "enum(%s)" % ','.join("'%s'" % k for (k, _) in self.choices)


# choices means data in string form to display in mysql and django admin interface

STATE =(('C' , 'C'),
            ('P', 'P),
            ('F, 'F'),
            ('F', 'F'),
            ('R', 'R'),)



# model for orderstate field 


class Table(models.Model):
    state = EnumField(choices=STATE,verbose_name ='orderstate')


now u can make your own enum field and any custom field by just edit some code in custom class
method -- >>  db_type is used for making db data type here it is enum    
and in __init__  --> you can customize your model and field in admin interface



Django Developer 



Monday, 24 September 2012

How to create dump .sql file in Mysql :) :)

First Start your command prompt or Terminal

now rum this command


mysqldump --opt --user=username --password pathofdumpfile