Snippet 7: adding plugins for endless-pagination, facebook login, autocomplete, and PIL

Super awesome email from my best bud Ant (shout-out here) to add several frequently used plugins for our django project. All these works to a T for me. Thanks Ant!

1) Added pagination (Django-endless-pagination)

Key feature: allow us to extract chunk by chunk from database and present it on front-end in pages. Clean display and efficient database extraction. Can possibly do the facebook endless scroll display too.

To get it go to terminal and type:

$ pip install django-endless-pagination

2) Added facebook login: (Django allauth)

To get it installed on ur local comp go to terminal and type:

$ pip install django-allauth

If that gives errors during runtime, den get addional support by going to terminal and type:

$ pip install -e git://

If that still fails, go to and set:

USE_TZ = False

3) Added an autocomplete plugin (django selectable)

Key feature: allow autocomplete from our database, works with foreignkey, manytomany etc..

To get it installed:

$ pip install django-selectable

4) Impt restructuring – images: Add PIL

First get python imaging library (PIL) installed:

Step 1 – Get JPEG support:

$ curl -O

$ tar zxvf jpegsrc.v8c.tar.gz

$ cd jpeg-8c/

$ ./configure

$ make

$ sudo make install

Step 2 – Get Freetype support:

curl -O

tar -xvzf freetype-2.4.5.tar.gz

cd freetype-2.4.5



sudo make install

cd ../


Step 3 – install PIL

$ pip install PIL

Images are now stored in the following fashion:

All images still have 1 link, for example: = “ngo_images/HueHelp/1.jpg”   (previously “/site_media/images_beta/ngo_images/HueHelp/1.jpg”)

You can access the different sizes of the image by concatenating that to the following:

/site_media/images_beta/thumbnail/ – 60px by 60px (~1-4kb)

/site_media/images_beta/small/ – 300px by 300px (~20kb)

/site_media/images_beta/large/ – 900px by 900px (~100-200kb)

/site_media/images_beta/original/ – whatever

So for example if you are passing an ngo object, and you want to display the link to its logo:

<img class=”ngo_avatar” src=”/site_media/images_beta/small/{{ngo.logo}}” alt=”{{}}”/>

Posted in Snippets | Leave a comment

Snippet 6: extending or specifying Model Instances

Refer to documentation here

More specifically for def __str__(self):

“The __str__() method is called whenever you call str() on an object. The main use for this method directly inside Django is when the repr() output of a model is displayed anywhere (for example, in debugging output). Thus, you should return a nice, human-readable string for the object’s __str__().” 


Screen Shot 2013-02-18 at 3.02.43 PM

Error: if you put an integer here it will throw a type error!

Screen Shot 2013-02-18 at 3.04.43 PM

The error would look like:

TypeError at /admin/
__str__ returned non-string (type

Solution is to use python’s conversion to string method  str():

Screen Shot 2013-02-18 at 3.04.59 PM

Posted in Snippets | Leave a comment

Snippet 5: Using fieldsets to customize admin page

We can use fieldsets to make admin page more organized:

1. In, under your class’s admin, create the fieldsets:
Screen Shot 2013-02-16 at 11.39.00 PM

Notice the brackets used in {  ‘fields’:[( A , B ), C , D]   }  This allows attributes grouped by () to be displayed within the same line:
Screen Shot 2013-02-16 at 11.42.40 PM

Here’s django’s documentation on fieldset


Posted in Snippets | Leave a comment

Snippet 4: Adding inline to admin for both ForeignKey and ManyToManyField

1. Foreign Key Method: Refer to Tutorial 4 (Part 2)

Note: using this same method for ManyToManyField will cause an error

2. ManyToManyField:

a) Under, create an inline class:
Screen Shot 2013-02-16 at 11.00.38 PM
Note: the class must be created before the admin class.. python compiles from top
Note: ‘vp’ is the variable name of the ManyToManyField in ‘Image’ that links both tables.. see the Image Class to understand:
Note: you must include ‘through’.. it tells django to use the table – that links VolProg and Image tables together – as a model to construct the inline class

Screen Shot 2013-02-16 at 11.03.54 PM
Screen Shot 2013-02-16 at 11.04.42 PM

b) of course, don’t forget the basics to import the admin, models, and register the admin
Screen Shot 2013-02-16 at 11.00.50 PM

Screen Shot 2013-02-16 at 11.00.57 PM

Here’s how it looks like:
Screen Shot 2013-02-16 at 11.42.52 PM

if you still don’t get this brief example, try this stackoverflow link

Posted in Snippets | Leave a comment

Snippet 3: Reset DB, See SQL statements, SyncDB

(python reset appName )  — reset whole database

( python sqlclear appName ) —- sneak peak DROP statements, use ( python manage,py dbshell ) to execute the drop statements

( python sql appName ) — sneak peak SQL statements to add TABLES

( python syncdb )   — sync database

Posted in Snippets | Leave a comment

Snippet 2: Dropping individual table

See this:

get the DROP statements with

python sqlclear <app name>

then try

python dbshell
and execute the DROP statement

And this:

Posted in Snippets | Leave a comment

Snippet 1: Understanding ForeignKey and M2M

Understanding ForeignKey and ManyToManyField in models.Model:

1. ForeignKey declares Many-To-1 relationship

– uses variable to access PrimaryKey

– puts ‘Many’ of itself as ‘set’ to PrimaryKey object

………………………………. \

VolProg (ForeignKey) ——- (PrimaryKey) Ngo

……………………………….. /

2. M2M declares Many-To-Many relationship

– uses variable.all to access all ‘Many’ PrimaryKeys

– puts ‘Many’ of itself as ‘set’ to PrimaryKey objects

………………………………. \ ……… /

VolProg (M2MField) ———- ———-(PrimaryKey) Ngo

……………………………….. / …….. \

Posted in Snippets | Leave a comment