Friday, 26 April 2013

Hack django admin tricks : 1

We can hide checkbox dispayed in django admin model templates

It's very simple hack u just go to ur admin.py file's respective class:
class Player(admin.ModelAdmin):
    list_display = ['gamename,'gametype]
    actions=None




Regards:
Prashant Gaur
+91 9030015491
91prashantgaur@gmail.com
Hyderabad

Saturday, 20 April 2013

Cool django admin tricks


1 ) Disable Add button in django admin interface for some models if they are having data in that table .


Answer :
Add below function in admin.py file in your respective class .

def has_add_permission(self,request):
        count = Data.objects.all().count()
        if count > 0:
            return False
        else:
            return True

2 ) disable delete operation for some models in django admin interface.

def has_delete_permission(self, request, obj=None):
        return False
 
 
 
 
3 ) Add link in django admin Interface .
 
list_display = ['my_url_field']   
    def my_url_field(self, obj):
        return '<a href="%s%s">%s</a>' % ('', obj.id, 'Click Here')
    my_url_field.allow_tags = True
    my_url_field.short_description = 'Data'
     

Saturday, 16 March 2013

send email when django admin is created

 
 
from django.db.models.signals import post_save
from django.contrib.auth.models import User

def send_user_email(sender, instance=None, **kwargs):
    if kwargs['created']:
        #your code here to send mail 
 
post_save.connect(send_user_email, sender=User)

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